0

For example I have an field that needs to have an user selected, so I have another React component where I select there the value, so I have a state there, how can I send that data to the current Component I have.

1
  • In case the data needs to be sent from parent to child component, one may be able to use props (NOTE: ReactJS uses uni-directional data-flow). If the components are not parent-child, another idea is to move the state up the hierarchy to a module where both components may receive the state. Another idea is to employ one of several state management tools (for example: redux). And, I'm sure there are other ways too (may be useContext hook, etc). Commented Mar 8, 2022 at 13:02

3 Answers 3

1

There are a few ways to do it.

Sign up to request clarification or add additional context in comments.

Comments

1

You can pass the value of that state as a prop:

const [selectedUser, setSelectedUser] = useState();
...
return (
  <OtherComponent prop={selectedUser} />
)

and receive that prop on the other component

export default function AnotherComponent(prop) {...}

Comments

1
  1. use props in share data between componet 2).use redux

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.