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.

Functions in useContext API

See original GitHub issue

Hello,

I have a Context API component to show/hide a modal. I’m using something very similar to the Toast example from here: Ponyfoo Toast

WDYR gives me this error: Captura de Tela 2019-06-03 às 10 39 51

How can I avoid this re-render? Or it is normal react behavior?

All functions are created using useCallback, I’ve already tried adding memo to both the <Header />(the component that I tested WDYR) and the <Context.Provider />

Any ideas?

Thanks in advance

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
vzaidmancommented, Jun 3, 2019

ok so here

const ModalManager = ({ children }) => {
  const { show, hideModal, showModal } = useModalManager();

  return (
    <ModalContext.Provider value={{ showModal, hideModal }}>
      {children}
      {show && <Modal onDismiss={hideModal} />}
    </ModalContext.Provider>
  );
};

{ showModal, hideModal } is a new object on every ModalManager re-render that triggers a re-render of every offspring that uses ModalContext

what i suggest you to do is something like:

const useModalManager = () => {
  const [show, setShow] = useState(false);

  const showModal = useCallback(() => {
    setShow(true);
  }, []);

  const hideModal = useCallback(() => {
    setShow(false);
  }, []);

  const actions = useMemo(() => ({
    showModal, hideModal
  }), [])

  return {
    show,
    actions,
  };
};

and then:

const ModalManager = ({ children }) => {
  const { show, actions } = useModalManager();

  return (
    <ModalContext.Provider value={actions}>
      {children}
      {show && <Modal onDismiss={actions.hideModal} />}
    </ModalContext.Provider>
  );
};

I didnt run it but i think you’ll get the idea behind it.

the main point is to make sure value is always exactly the same object and not a new one on every ModalManager re-render

1reaction
shahafacommented, Jun 4, 2019

IMHO I think it is better to put the useMemo outside of the hook in ModalManager since it’s not related to the hook logic and more robust

const useModalManager = () => {
  const [show, setShow] = useState(false);

  const showModal = useCallback(() => {
    setShow(true);
  }, []);

  const hideModal = useCallback(() => {
    setShow(false);
  }, []);

  return {
    show,
    showModel,
    hideModel
  };
};
const ModalManager = ({ children }) => {
  const { show, showModel, hideModel } = useModalManager();

  const actions = useMemo(() => ({
    showModal, hideModal
   }), [showModal, hideModal])
  
  return (
    <ModalContext.Provider value={actions}>
      {children}
      {show && <Modal onDismiss={hideModal} />}
    </ModalContext.Provider>
  );
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks API Reference - React
They let you use state and other React features without writing a class. This page describes the APIs for the built-in Hooks in...
Read more >
How can ı pass function with state for react useContext
The React Context is a mechanism for passing data through a React node tree without having to pass the props manually.
Read more >
A Guide to React Context and useContext() Hook
The React context provides data to components no matter how deep they are in the components tree. The context is used to manage...
Read more >
How To Work with Context API in React and React Hooks
Your return statement applies the background color in your application. When a change triggers, the useContext() method will subscribe update ...
Read more >
React useContext Hook - W3Schools
React Context is a way to manage state globally. It can be used together with the useState Hook to share state between deeply...
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