Skip to content
Youngho Chaa cha cha
TwitterHomepage

React Tips

react, tips1 min read

Append the new data to the existing state

Use function in set state

useEffect(() => {
if (graphData) {
setNodes(prevNodes => [...prevNodes, ...graphData.nodes]);
setLinks(graphData.links);
}
}, [graphData]);

Conditionally join class names

Use classnames: https://github.com/JedWatson/classnames

classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'
© 2024 by Youngho Chaa cha cha. All rights reserved.
Theme by LekoArts