1

I am beginner with React technology, here I am trying to select a language value from dropdown and want to use that value in another component.Can someone help me with this?

export default class Language extends Component {
    constructor(props) {
        super(props);
        this.state = {value:'se'};
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }

      handleChange(event) {
        this.setState({value: event.target.value});
      }

      handleSubmit(event) {

        alert('Language selected is: ' + this.state.value);
        event.preventDefault();
      }
      renderSelectOptions (item){ 
        <option key={item} value={item}>{item}</option>
        alert('hii' + item);
      }
    render () {
      return (
        <div>
             <select value={this.state.value} onChange={this.handleChange}>
             {this.props.value(this.renderSelectOptions)}
              <option itemText="Swedish" value="se">Swedish</option>
              <option itemText="English" value="en">English</option>
              <option itemText="French" value="fr">French</option>
          </select>
          </div>
      )
    }
  }
btvs
  • 35
  • 6
  • You seem to already have the `onChange` part done. You select a value and save it in the state. To send it to another component just pass the state as a property of said component `` – Rodius Jun 18 '18 at 11:00
  • user redux https://redux.js.org/basics/usage-with-react – Harish Soni Jun 18 '18 at 11:00
  • @Rodius..thanks for your reply..you mean to say..I should have one more method like onSave to save the state? – btvs Jun 18 '18 at 11:09
  • You are essentially trying to notify the parent that a child has changed so that the parent can in turn notify a different child about the change. See https://stackoverflow.com/a/40644768/173225. Since you are a beginner, I suggest you work through it with just React. _After_ you have it working with a basic understanding, _then_ take a look at Redux or similar. Redux is awesome, but it's not the answer to everything – Colin Young Jun 18 '18 at 12:44

0 Answers0