简体   繁体   English

在使用ES2016的React中的箭头功能中调用setState时未安装组件

[英]Component not mounted when setState called in arrow function in React with ES2016

Note: I have restructured my question to make it more understandable. 注意: 我重新构建了我的问题,使其更容易理解。 However, I have kept the older version for historical purposes. 但是,我保留旧版本用于历史目的。

I have a React application and when I call this.setState() from an arrow function, I get the "component not mounted message". 我有一个React应用程序,当我从一个箭头函数调用this.setState() ,我得到“组件未挂载消息”。 Here is my component: 这是我的组件:

import React, { Component } from 'react'

class Test extends Component {
    state = {
        value: ''
    }

    onChange = (e) => {
        e.preventDefault()
        this.setState({
            value: e.target.value
        })
    }

    render() {
        return <input value={ this.state.value } onChange={ this.onChange } />
    }
}

When I type into the input, I get the following error message: 当我输入输入时,我收到以下错误消息:

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.

I am not sure why this is, because the arrow function should not have to be binned, and since this is triggered by typing, the component is obviously mounted. 我不知道为什么会这样,因为箭头函数不应该被分箱,并且因为这是通过键入触发的,所以组件显然是被挂载的。

Thanks for any help! 谢谢你的帮助!


Here is the previous version of this question: 这是此问题的先前版本:

I have a pretty standard React application and when I call this.setState() from an arrow function, I get the "component not mounted message". 我有一个非常标准的React应用程序,当我从一个箭头函数调用this.setState() ,我得到“组件未挂载消息”。

The code looks something like this: 代码看起来像这样:

 onClick = () => { this.setState({clicked: true}) } ... render() { return <AnotherComponent onClick={ this.onClick } /> } 

When onClick is called, I get this message: 当调用onClick ,我收到以下消息:

 Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. 

I'm not sure what is causing this, because I am using an arrow function but it is still not working. 我不确定是什么导致这种情况,因为我使用箭头功能,但它仍然无法正常工作。

EDIT: As asked, here is AnotherComponent : 编辑:如所问,这是AnotherComponent

 const AnotherComponent = (props) => { return ( <div> <Button primary>Reject</Button> <Button primary onClick={ props.onClick }>Edit Post</Button> <Button primary>Approve</Button> </div> ) } 

Thanks 谢谢

When you say onClick={ this.onClick } you are saying "here is a function to use". 当你说onClick={ this.onClick }你会说“这是一个可以使用的功能”。 However, it loses its context. 然而,它失去了它的背景。

You need to bind() the context of onClick() before you pass it into your attribute. bind() onClick()传递给属性之前,需要bind() onClick() bind()的上下文。 The best place to do this is in the constructor of your class. 最好的地方是在你班级的constructor中。

class MyClass extends React.Component {
     constructor(props) {
       super(props); // have to do this part in any Component constructor
       this.onClick = this.onClick.bind(this);
     }

     onClick() {
       this.setState({ clicked: true });
     }

     render() {
       return <AnotherComponent onClick={ onClick } />;
     }
}

Alternatively, if your function is short, you can just create it in the constructor: 或者,如果您的函数很短,您可以在构造函数中创建它:

constructor(props) {
  super(props);

  this.onClick = () => { this.setState({ clicked: true }); };
}

By being creating in the context of the constructor, it'll implicitly be bound. 通过在构造函数的上下文中创建,它将被隐式绑定。

Your component is perfectly correct, now we only have to know where are you calling it from. 你的组件是完全正确的,现在我们只需要知道你在哪里调用它。

This issue usually comes from the parent, just check if the parent is fully mounted. 此问题通常来自父级,只需检查父级是否已完全装入。

Matter of fact: 事实上:

  • onChange doesn't need the e.preventDefault() , onChange不需要e.preventDefault()
  • as each time you type on the input, it invokes setting the state , 每当你输入输入时,它会调用设置state
  • every time the state gets set, 每次state设定,
  • It triggers the lifecycle method render() again ( it reloads the node), so in this case. 它再次触发生命周期方法render() (它重新加载节点),因此在这种情况下。
  • when you preventDefault() , you're breaking the regular flow of lifecyle components :) remove it and the warning will go away. 当你preventDefault() ,你打破了生命周期组件的常规流程:)删除它,警告将消失。

====================PART 2 =========================== ====================第2部分===========================

Oh I see now, everything is clear :D 哦,我现在看,一切都很清楚:D

Whenever you call the onClick = () => {
    this.setState({clicked: true})
}

...

render() {
    return <AnotherComponent onClick={ this.onClick } />
}

You're wanting to pass down the function through props but you're setting it to the onClick event, and it's a reserved word 你想通过props传递函数,但是你将它设置为onClick事件,它是一个保留字

You should pass it through a reserved prop like: 你应该通过一个保留的道具传递它,如:

return <AnotherComponent handleClick={ this.onClick } />

and then on the child: 然后对孩子:

<Button primary onClick={ props.handleClick }>Edit Post</Button>

now you created a an actual property 现在你创建了一个实际的属性

Hope it helped you :) 希望它能帮助你:)

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

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