V
V
Vanya Huk2018-03-20 01:16:58
React
Vanya Huk, 2018-03-20 01:16:58

Why, when I update the state in the parent component, in the child props remain the same?

here in the parent component I pass data as props to the child

<FilteredList
                                        label={this.props._sharedData.loc.advanced_search.select_brand}
                                        data={this.state.brands}
                                    />

child code
constructor(props) {


        super(props);
        
        this.state = {

            data: this.props.data,

            filteredData: this.props.data

        };


        console.log( this.state )

    }

here is the output to the console of the child element
5ab0367191c5b679508528.png
and here is the date that I pass,
5ab036c6ac870603660437.png
i.e. array is not empty

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-03-20
@vanyahuk

You set state in the constructor. The constructor is called only once, when the component is initialized.
this.props.data is updated, it's just that you probably don't use it.
As far as I understand your task, it makes no sense for you to write props in state . Just use props directly in the render method and that's it. If you need to change them for any event, then add an additional handler from the parent.
Try to avoid pasta of the form:

label={this.props._sharedData.loc.advanced_search.select_brand}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question