How can I return multiple useState or functions with custom hook?
See original GitHub issueHi @dai-shi , I’m very interested about your library I also used to use react-global-state-hook. Now I move to React-tracked and I have a question about it I want to return count1 and setCountOrig1, delPosts also. What can I do? thanks you
//custom hook I used to do like this before:
return {
delPosts ,
count,
setCoun,
count1,
setCountOrig1
};
Example :
import { createContainer } from 'react-tracked';
const useValue = () => {
const [count, setCountOrig] = useState(0);
const [count1, setCountOrig1] = useState(0);
const setCount = (nextCount: number) => {
// eslint-disable-next-line no-console
console.log({ nextCount });
setCountOrig(nextCount);
};
//delete posts
const delPosts = useCallback(
async id => {
const newData = filter(dataPosts?.data, o => o.id !== id);
mutatePosts({ ...dataPosts, data: [...newData] });
await api.delPosts(id);
//refetch
refreshPosts();
},
[dataPosts, mutatePosts, refreshPosts]
);
return [count, setCount] as const;
const { Provider, useTracked } = createContainer(useValue);
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Better aproach for destructing Custom React Hooks with ...
Is there a better aproach for destructing Custom React Hooks with multiple returns than remembering the order of the returns and using alot...
Read more >Writing Your Own React Hooks, the Return Value
We can return multiple values in arrays or objects from a function and instantly destructure them. And, as you know, a React hook...
Read more >Rules of Hooks
They let you use state and other React features without writing a class. Hooks are JavaScript functions, but you need to follow two...
Read more >5 Ways to Simplify Your React Hooks - Level Up Coding
When I first started writing custom hooks it was easy to follow a return style similar to the default `useState` hook. Although this...
Read more >Implementing React Custom Hooks: A Complete Guide
When the user selects a different friend in the <select> picker, we use the useState Hook to update the recipientID. The useFriendStatus Hook...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@seanblonien Would you quickly review #127? Yeah, I too noticed that
useCallback
is necessary.Great. And, I noticed the subtle concurrent rendering support is “off” by default 😓 , because it behaves a little weirdly in the legacy mode.
So, I think it’s worth documenting as a recipe.