[英]React: this.state disappears in for loop
How can I carry this
into my .map()
loop? 我怎么能把
this
带到我的.map()
循环中? It seems to disappear. 它似乎消失了。 :-(
:-(
I'm creating a "dynamic form" where the user can specify multiple lines of input for his form. 我正在创建一个“动态表单”,用户可以为其表单指定多行输入。 I want to iterate over all items in
state.items[]
and build form input fields for them. 我想迭代
state.items[]
所有项目,并为它们构建表单输入字段。
Eg the form starts with 'field' and 'autocomplete_from. 例如,表单以“field”和“autocomplete_from”开头。 The user can then click add a new line to get more rows in his form.
然后,用户可以单击添加新行以在其表单中获取更多行。
102 render: function() {
103 return (
104 <div>
105 {this.state.items.map(function(object, i){
106 return (
107 <div>
109 <FieldName/>
110 <strong> State.autocomplete_from:
{this.state.autocomplete_from} </strong>
// ^^^
// Uncaught TypeError: Cannot read property 'state' of undefined
120 <button onClick={this.newFieldEntry}>Create a new field</button>
121 <button onClick={this.saveAndContinue}>Save and Continue</button>
122 </div>
123 );
124 })}
125 </div>
126 );
In .map
this does not refer to your component., there are several ways how you can solve this issue 在
.map
这并不是指你的组件。有几种方法可以解决这个问题
Save this
to variable 保存
this
变量
render: function() { var _this = this; return ( <div> {this.state.items.map(function(object, i){ return ( <div> <FieldName/> <strong> State.autocomplete_from: {_this.state.autocomplete_from} </strong> <button onClick={this.newFieldEntry}>Create a new field</button> <button onClick={this.saveAndContinue}>Save and Continue</button> </div> ); })} </div> ); }
Set this
for .map
callback( if you can't use ES2015
features, this variant is prefered ) 为
.map
回调设置this
( 如果您不能使用ES2015
功能,则首选此变体 )
this.state.items.map(function (object, i) { // .... }, this);
use arrow function
使用
arrow function
this.state.items.map((object, i) => { // .... })
use .bind
使用
.bind
this.state.items.map(function(object, i) { // .... }.bind(this))
second argument for iterative method such as map
and filter
is this
object 迭代方法的第二个参数,例如
map
和filter
就是this
对象
so you can use it as follows: 所以你可以按如下方式使用它:
this.state.items.map(yourfunction,this)
When you call a function. 当你调用一个函数。 This is set to the global object unless it's a member method you are invoking or you are invoking with either
.call
or .apply
which you can't in your case. 这是除非它是要调用一个成员方法设置为全局对象或您正在使用或者调用
.call
或.apply
你不能在你的情况。
or in other words you can't close over this
you can however closer over a standard variable to which you have assigned this. 或者换句话说,你不能关闭它,
this
你可以更接近你已经分配给它的标准变量。 So in general if you have a function nested inside another funtion and you wish to refere to this do: 所以一般来说,如果你有一个嵌套在另一个函数中的函数,你希望参考这个:
function outer(){
var _this = this;
someThingThatAcceptsACallback(function(){
console.log(_this.state);
}
}
If you're using Babel or another modern EcmaScript6->JS5 compiler, you can use a simpler syntax for preserving the context of the outer context: 如果您正在使用Babel或其他现代EcmaScript6-> JS5编译器,则可以使用更简单的语法来保留外部上下文的上下文:
{
this.state.items.map((object, i) => {
return ( <div>
<strong> State.autocomplete_from:
{this.state.autocomplete_from}
</strong>
/*.... your code ...*/ </div>);
});
}
By using the arrow function syntax, the this
context is bound automatically within the contained function, so you can use this
as you already were and you don't need to do anything special in your code. 通过使用箭头函数语法,
this
上下文在包含的函数中自动绑定,因此您可以像以前一样使用this
,并且不需要在代码中执行任何特殊操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.