Ad

React Native How Get Variable From Reusable Component

i want to reuse this component

render() {
    return (



        <SelectionGroup
            renderContent={this.renderButton}
            items={this.props.items}
            onPress={this.selectionHandler.selectionHandler}
            isSelected={this.selectionHandler.isSelected}
            containerStyle={styles.answers}
            onItemSelected={(item) => this.setState({ selectedAnswer: item.value })}
        />



    );}

from home screen i manage to send the items to selection group

import Selection from './selectionGroup'

      <Selection
      items={cofeeType.options}
      />

my problem is i don't know how to get back the selectedAnswer from the reusable component

Ad

Answer

you have to send a call back for setting value of your parent state

constructor(props){
  super(props)
  this.state={
    answers=[]
  }
}

handleAnswers=(answers)=>{
  this.setState({answers})
}

      <Selection
      handleAnswers={this.handleAnswers}
      items={cofeeType.options}
      />

in Component handle the callback

handleValue=(answers)=>{
  const {handleAnswers}=this.props;
  handleAnswers(answers)
  this.setState({ selectedAnswer: item.value });
}

render() {

  return (



      <SelectionGroup
          renderContent={this.renderButton}
          items={this.props.items}
          onPress={this.selectionHandler.selectionHandler}
          isSelected={this.selectionHandler.isSelected}
          containerStyle={styles.answers}
          onItemSelected={this.handleValue}
      />



  );}
Ad
source: stackoverflow.com
Ad