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.
CodePudding user response:
There are a few ways to do it.
- Lifting state up - Documentation Link
- Using React Context API - Documentation Link
- adding that selected data to LocalStorage (can be done but not suggested) - Article Link
CodePudding user response:
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) {...}
CodePudding user response:
- use props in share data between componet 2).use redux