blog/dont-over-use-state #45
Replies: 9 comments 5 replies
-
Thank you, But assuming we have to fetch an API call for the categories based on data, we still need that useEffect |
Beta Was this translation helpful? Give feedback.
-
For data fetching, i can recommend react-query 😉 |
Beta Was this translation helpful? Give feedback.
-
@danialdezfouli and if not react-query, IMO just use both fetches in one useEffect |
Beta Was this translation helpful? Give feedback.
-
I noticed the "Thinking in React" from the old React docs made it in to the new beta docs too. That's good because it's one of the best intro's to React IMO. I did notice a small change though.
Was changed to:
Notice the differences? The 'probably's are gone and a definitely was added: "If so, it definitely isn’t state!" Ambiguity be gone! And rightfully so. I think the React docs team realized this is a common mistake, as affirmed by your blogpost. |
Beta Was this translation helpful? Give feedback.
-
oh right, that is a pretty sneaky, but solid change. I wrote the blogpost because I've seen it done "wrong" every so often, and yes, I'm sure the react team has also seen that. It's important to communicate that understanding 🙌 |
Beta Was this translation helpful? Give feedback.
-
Could there be an exception to your postulation? If function App() {
const [data, setData] = React.useState(null);
const [categories, setCategories] = React.useState<string[]>([]);
React.useEffect(() => {
async function fetch() {
const response = await fetchData();
setData(response.data);
}
fetch();
}, []);
React.useEffect(() => {
if (data) {
setCategories(computeCategories(data));
}
}, [data]);
return (
<>
...
<input
value={categories.join()}
onChange={e => setCategories([e.target.value])}
/>
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for this nice article... I was just wondering if I can just simplify everything like this: const App = () => {
const [data, setData] = React.useState([]);
React.useEffect(() => {
async function fetch() {
const response = await fetchData();
setData(computeCategories(response.data));
}
fetch();
}, []);
return <>...</>;
}; |
Beta Was this translation helpful? Give feedback.
-
Good writeup, thanks for writing this. I personally think of as less state as possible and if your component has too many state related operations then it should not have JSX. Whole rendering should be pushed to a new component which just reflects on props. More like Controller and View layer. And if there are too many useState, can you combine them in a useReducer; this forces me to think of state derivatives in terms of data instead of React. |
Beta Was this translation helpful? Give feedback.
-
I love this quote <3. Don't prematurely optimize, always measure first. |
Beta Was this translation helpful? Give feedback.
-
Don't over useState | TkDodo's blog
The first part of the useState pitfalls series is all about avoiding state altogether. Lots of things might not even be state after all...
https://www.dorfmeister.cc/blog/dont-over-use-state
Beta Was this translation helpful? Give feedback.
All reactions