Skip to content

Pass Selected Value to Parent Component #4555

Answered by ebonow
bluecrunch asked this question in Q&A
Discussion options

You must be logged in to vote

Greetings @bluecrunch ,

React advocates a one way data flow, so the basic methodology for something like this would be to create the Select as a controlled component in which the parent provides both the value and setValue method as props to the child.

// find.js
const Find = props => {
  const { selectedValue, setSelectedValue } = props;

  const onChange = option => {
    // do stuff with selected object and then send selected value to parent through provided prop
    setSelectedValue(option);
  }
  
  return <Select value={selectedValue} onChange={onChange} />
}

// items.js
const Items = props => {
  const [ selectedValue, setSelectedValue ] = useState();
  
  return (
    <Find selec…

Replies: 1 comment 1 reply

Comment options

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

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