react-enlightenment icon indicating copy to clipboard operation
react-enlightenment copied to clipboard

confusing props

Open justinobney opened this issue 7 years ago • 1 comments

One of the things that confused me when I was getting into React was the difference between props and state. I think passing down a prop called state could potentially trip up others as well...

in: what-is-react.md

Using props, again, we will pass the selected state from the <MySelect> component down to the <MyOption> component by placing the property state={this.state.selected} on all of the <MyOption> components. Now that we know the state (i.e., this.props.state) and the current value (i.e., this.props.value) of the option we can verify if the state matches the value. If it does, we then know that this option should be selected. This is done by writing a simple if statement which adds a styled selected state (i.e., selectedStyle) to the JSX

if the state matches the value of the current option. Otherwise, we return a React element with unSelectedStyle styles.

state={this.state.selected} could be a little clearer as selected={this.state.selected}

justinobney avatar Aug 05 '16 19:08 justinobney