question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I return multiple useState or functions with custom hook?

See original GitHub issue

Hi @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:closed
  • Created 3 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Dec 20, 2021

@seanblonien Would you quickly review #127? Yeah, I too noticed that useCallback is necessary.

1reaction
dai-shicommented, Dec 20, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found