Skip to content

Help me understand useObserver vs observer HoC #2709

Answered by danielkcz
KubiGR asked this question in Q&A
Discussion options

You must be logged in to vote
function useGlobalState() {
  const stores = useStores();
  return useObserver(() => ({
    todos: stores.childStore.todos
  }));
}

export const TodosContainer = () => {
  const { todos } = useGlobalState();
  return (
    <>
      <h1>TODOS</h1>
      {todos.map((todo) => (
        <Todo key={todo.id} todo={todo} />
      ))}
    </>
  );
};

This is exactly the antipattern that we wanted to get rid of and why we deprecated useObserver. If you use it like this then you are observing only what's wrapped in the useObserver, in your case nothing. You are only returning a reference to a list of todos. Only the actual iteration over it and accessing those will trigger reactivity, but there is…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@danielkcz
Comment options

Answer selected by KubiGR
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants