Skip to content

confusing props #5

@justinobney

Description

@justinobney

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 component down to the component by placing the property state={this.state.selected} on all of the 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}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions