Skip to content

Why Prop Destructuring is Not Recommended #2195

Answered by atk
MinJieLiu asked this question in Q&A
Discussion options

You must be logged in to vote

Let me elaborate then. Consider you have a component that takes a prop. The parent feeds that prop from a store. Currently, it looks like this:

function ParentComponent() {
  const [store, setStore] = createStore({ bar: "works" });
  return <ChildComponent foo={store.bar} />;
}
function ChildComponent(props) {
  return <p>{props.foo}</p>;
}

To keep it reactive, we would have to make a function out of the prop:

function ParentComponent() {
  const [store, setStore] = createStore({ bar: "works" });
  return <ChildComponent foo={() => store.bar} />;
}
function ChildComponent({ foo }) {
  return <p>{foo()}</p>;
}

Since it is easier to forget always passing functions than it is to remind not t…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@MinJieLiu
Comment options

@atk
Comment options

Answer selected by MinJieLiu
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