Passing and updating prop / state values from parent with multiple components in React JS

Passing and updating prop / state values from parent with multiple components in React JS

I have two components, a select list with several options in one component and a button in another component.

I have two components, a select list with several options in one component and a button in another component.

What I would like to in the UI is that you can select any option from the select list and also that you can click the button to reset the select list back to 'select'.

Both of these components are inside the Parent component which is what is rendered - I am trying to keep the parent as the 'single source of truth' so that the select list can update its value when selected and then the reset button can also update this same value.

I've written how I think this should work but the select list is stuck on the first item and the reset button doesn't update the list, can't spot where I've gone wrong, if anyone can give me some advice / point me in the right direction I'd be very grateful - thanks

class SelectList extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

handleChange(e) { this.props.onSelectListChange(e.target.value); }

render() { const selectedValue = this.props.selectedValue; return ( <select value={selectedValue} onChange={this.handleChange}> <option value='One'>One</option> <option value='select'>select</option> <option value='Three'>Three</option> <option value='Four'>Four</option> <option value='Five'>Five</option> </select> ); } }

class SelectListReset extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); }

handleChange(e) { this.props.onSelectListChange(e.target.value); }

render() { return ( <div> <button onClick={this.handleChange}>Reset list to select</button> </div> ); } }

class Parent extends React.Component { constructor(props) { super(props); this.handleSelectedListChange = this.handleSelectedListChange.bind(this); }

handleSelectedListChange(selectedValue) { this.setState({selectedValue}); }

handleResetChange(selectedValue) { this.setState({selectedValue: 'select'}); }

render() { return ( <div> <SelectList onSelectListChange={this.handleSelectedListChange} selectedValue={this.handleSelectedListChange} /> <SelectListReset handleResetChange={this.handleSelectedListChange} /> </div> ); } }

ReactDOM.render( <Parent />, document.getElementById('root') );


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project. This video covers the key differences between ReactJS, Angular and Vue with respect to the following: Use case, Performance, Data binding, Scripting language, Testing, Community support, Growth curve

What is JavaScript – All You Need To Know About JavaScript

In this article on what is JavaScript, we will learn the basic concepts of JavaScript.

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.