Giter Site home page Giter Site logo

Comments (6)

vzaidman avatar vzaidman commented on May 9, 2024 2

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

from why-did-you-render.

vzaidman avatar vzaidman commented on May 9, 2024 1

a hook makes components re-render "from the inside" of the component, so MEMO won't help.

what you need to do is to ensure, what ever passes

{hideAddressModal, showAddressModal}

is memoized.

or everytime you get to <Context hideAddressModal=...... />
every single component that depends on this context will be re-rendered. including pure components.

You can, for example, wrap it in a Memoized component.

const MemoizedContext = React.memo(
  () => <Context ... />
)

from why-did-you-render.

shahafa avatar shahafa commented on May 9, 2024 1

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>
  );
};

from why-did-you-render.

Schiavi avatar Schiavi commented on May 9, 2024

@vzaidman Thank you for your answer.

I've created a very simple CRA app to show how I'm doing it. My context already has a memo around it, and even still, it re-renders the <Header> every time.

Here is the repo

Any suggestions on what I'm doing wrong?

Thanks again.

from why-did-you-render.

Schiavi avatar Schiavi commented on May 9, 2024

@vzaidman That worked like a charm! Thank you very much. =)

from why-did-you-render.

vzaidman avatar vzaidman commented on May 9, 2024

Sure :)

from why-did-you-render.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.