3 ms·
The initial writing for "useAsyncExtendedState" said "Don't use extendState for everything! Use it only when you know you need to merge a partial state.". Howev
by nightpool 5y ago
The initial writing for "useAsyncExtendedState" said "Don't use extendState for everything! Use it only when you know you need to merge a partial state.". However, the example doesn't use "setState" at all. This feels like a gap or tension in the API design—if setState doesn't get used in practice, why force users to include it? Instead, I think the default react setState pattern is much better, since it allows you to easily set *or* extend your state depending on your usecase:
const [state, setState] = useState({});
// replace
setState({foo: 1, bar: 2});
// extend
setState(currentState => ({...currentState, foo: 1}));
- loh 5y agoIn the examples, `extendState` is used because the API returns only the updated props when updating. Your preference for only `setState` is definitely warranted. The source is available for this reason, and it's pretty compact. You can quickly remove the `extendState` portion if you want.
- deleted 5y ago[deleted]