简体   繁体   English

如何通过解构访问组件状态?

[英]How can I access my component state through destructuring?

I'm trying to access the this.state.timeRemaining value from within the componentWillMount() function. 我正在尝试从componentWillMount()函数内部访问this.state.timeRemaining值。 I've destructured the this.state object and renamed the value to "swag". 我已经分解了this.state对象,并将值重命名为“ swag”。 I expect my console.log() statement to print out "5" (as I have set the state and run this print statement in the callback function) but the value of "null" is printed instead. 我希望我的console.log()语句打印出“ 5”(因为我已经设置了状态并在回调函数中运行了该打印语句),但是却打印了“ null”的值。 I believe this is a destructuring specific issue as I am able to print "5" by using this.state.timeRemaining in the console.log() statement instead. 我相信这是一个破坏性的特定问题,因为我可以通过在console.log()语句中使用this.state.timeRemaining来打印“ 5”。 Any ideas why this is? 任何想法为什么会这样? Does this have anything to do with the context? 这与上下文有关系吗?

class Favr extends Component {

    constructor(props) {
        super(props);
        this.state = {detailsAreShowing: false, timeRemaining: null};
        this.showDetails = this.showDetails.bind(this);
    }

    componentWillMount() {
        const { timeRemaining: swag } = this.state;
        const { favr: {expirationTime} } = this.props;
        let remainingTimeInMil = expirationTime.getTime() - Date.now();
        this.setState({timeRemaining: 5}, () => {
            console.log(swag); // prints null
        });
        ...
    }
    ...
}

That's because you are reading the declared variable at the first line of componentWillMount method which it's not updated. 这是因为您正在componentWillMount方法的第一行读取声明的变量,该变量未更新。 Even if you do not rename it it will print null as well. 即使您不重命名,它也将输出null When you read this.state.timeRemaining again it provides the updated value. 当您再次阅读this.state.timeRemaining时,它将提供更新的值。

There is a problem with your understanding and using of variables and references in JS. 您对JS中的变量和引用的理解和使用存在问题。

  • By destructuring / deconnstructing const {timeRemaining: swag} = this.state , you are creating a new variable swag . 通过解构/解构const {timeRemaining: swag} = this.state ,您将创建一个新变量swag This variable will have new memory allocated to it, updating timeRemaining will not change value of swag as at the time of assignment, timeRemaining has value of null . 此变量将分配有新的内存,更新timeRemaining不会像分配时一样更改swag值, timeRemaining值为null Assignment by referencing only works with object in JS. 通过引用分配仅适用于JS中的object

  • Also, not directly related to your question, but it's worth knowing that it's never a good idea to use componentWillMount with React . 另外,与您的问题没有直接关系,但是值得一提的是,将componentWillMountReact一起使用绝不是一个好主意。 This lifecycle method has been deprecated since React 16.3: https://reactjs.org/docs/react-component.html#unsafe_componentwillmount . 从React 16.3开始不推荐使用此生命周期方法: https : //reactjs.org/docs/react-component.html#unsafe_componentwillmount

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

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