简体   繁体   English

反应componentWillReceiveProps不更新状态

[英]React componentWillReceiveProps not updating state

I've got this React parent component here. 我在这里有这个React父组件。 The children components at this point are just returning dropdown menus. 此时的子组件只是返回下拉菜单。 I expected that componentWillReceiveProps would update the state here, which in turn should be passed to StopList as props. 我希望componentWillReceiveProps将在这里更新状态,而状态又应作为道具传递给StopList。 However, when state.selectedSub is changed through handleSubSelect, nothing happens and StopList doesn't receive any props. 但是,当通过handleSubSelect更改state.selectedSub时,什么也没有发生,并且StopList不接收任何道具。

Is my mistake with the asynchronous nature of componentWillReceiveProps? 我的错误是componentWillReceiveProps的异步特性吗? Is it in the wrong place in my code? 它在我的代码中的错误位置吗? Am I using the wrong lifecycle method? 我是否使用了错误的生命周期方法?

// We're controlling all of our state here and using children
// components only to return lists and handle AJAX calls.

import React, { Component } from 'react';
import SubList from './SubList';
import StopList from './StopList';

class SubCheck extends Component {

  constructor (props) {
    super(props);
    this.state = {
        selectedSub: '--',
        selectedStop: null,
        stops: ['--'],
    };
    this.handleSubSelect.bind(this);
    this.handleStopSelect.bind(this);
    }

    // We want the user to be able to select their specific subway
    // stop, so obviously a different array of stops needs to be 
    // loaded for each subway. We're getting those from utils/stops.json.
    componentWillReceiveProps(nextProps) {
        var stopData = require('../utils/stops');
        var stopsArray = [];
        var newSub = nextProps.selectedSub
        for(var i = 0; i < stopData.length; i++) {
            var stop = stopData[i];

            if (stop.stop_id.charAt(0) === this.state.selectedSub) {
                stopsArray.push(stop.stop_name);
            }
        }
        if (stopsArray.length !== 0 && newSub !== this.state.selectedSub) {
            this.setState({stops: stopsArray});
        }
    }

    handleSubSelect(event) {
        this.setState({selectedSub:event.target.selectedSub});
    }

    handleStopSelect(event) {
        this.setState({selectedStop:event.target.selectedStop})
    }

    render() {
        return (
            <div>
                <SubList onSubSelect={this.handleSubSelect.bind(this)}/>
                <StopList stops={this.state.stops} onStopSelect={this.handleStopSelect.bind(this)}/>
            </div>
        );
    }
}

export default SubCheck;

You are duplicating data, and causing yourself headaches that aren't necessary. 您正在复制数据,并导致不必要的头痛。

Both selectedSub and selectedStop are being stored as props and as state attributes. selectedSubselectedStop都存储为propsstate属性。 You need to decide where this data lives and put it in a singular location. 您需要确定这些数据的存放位置,并将其放置在单个位置。

The problem you are encountering entirely revolves round the fact that you are changing the state attribute and expecting this to trigger a change to your props. 您所遇到的问题完全围绕着以下事实:您正在更改state属性,并期望这会触发对道具的更改。 Just because they share a name does not mean they are the same value. 仅仅因为它们共享一个名称并不意味着它们具有相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM