Javascript React类:引用类作为";这",;,在对象内';s函数性质

Javascript React类:引用类作为";这",;,在对象内';s函数性质,javascript,reactjs,methods,this,jsx,Javascript,Reactjs,Methods,This,Jsx,我终于开始使用react和ES6了,它进行得很顺利,但我最终还是被难住了,我需要一些指导 我已经开始着手将this绑定到一个方法来引用该类,但我正在尝试更深入一点。以这一点为例……其效果与预期一致: class App extends Component { state = { myFirstState: false, }; handleMyFirstState = () => { this.setState( { myFirst

我终于开始使用react和ES6了,它进行得很顺利,但我最终还是被难住了,我需要一些指导

我已经开始着手将
this
绑定到一个方法来引用该类,但我正在尝试更深入一点。以这一点为例……其效果与预期一致:

class App extends Component {

    state = {
        myFirstState: false,
    };

    handleMyFirstState = () => {
        this.setState( { myFirstState : true } );
    };

    render() {

        return (
            <MyComponent handleMySate={ this.handleMyState } />
        );

    }

}

export default App;

首先,在第二个示例中,您将
this.handleStates
作为prop
handleStates
的值传递,但它是未定义的。您将
handleStates
构建为局部变量,因此希望道具引用该局部变量:

<MyComponent handleStates={handleStates} />
它具有更新
App
中状态的预期结果

值得一提的是,有一种更常见的模式:只需传递一个更新程序函数作为道具,根据它的功能命名:

class Sandwich extends React.Component {
  this.state = {
    bread: "",
    meat: "",
    veggie: "",
  }

  updateSandwich = (component, selection) => {
    this.setState({ [component]: selection })
  }

  render() {
    return(<IngredientSelector updateSandwich={this.updateSandwich} />)
  }
}

class IngredientSelector extends React.Component {
  return(){
    <button value="Rye" onClick={() => this.updateSandwich("bread", "rye")} />
    <button value="Wheat" onClick={() => this.updateSandwich("bread", "wheat")} />
    <button value="Ham" onClick={() => this.updateSandwich("meat", "ham")} />
    <button value="Turkey" onClick={() => this.updateSandwich("meat", "turkey")} />
  }
}
class.Component{
此.state={
面包:“,
肉类:“,
素食者:“,
}
updateSandwich=(组件,选择)=>{
this.setState({[component]:selection})
}
render(){
返回()
}
}
类InCreditSelector扩展了React.Component{
返回(){
this.updateSandwich(“面包”、“黑麦”)}/>
这个.updateSandwich(“面包”、“小麦”)}/>
this.updateSandwich(“肉”、“火腿”)}/>
this.updateSandwich(“肉”、“火鸡”)}/>
}
}

首先,在第二个示例中,您将
this.handleStates
作为prop
handleStates
的值传递,但它未定义。您将
handleStates
构建为局部变量,因此希望道具引用该局部变量:

<MyComponent handleStates={handleStates} />
它具有更新
App
中状态的预期结果

值得一提的是,有一种更常见的模式:只需传递一个更新程序函数作为道具,根据它的功能命名:

class Sandwich extends React.Component {
  this.state = {
    bread: "",
    meat: "",
    veggie: "",
  }

  updateSandwich = (component, selection) => {
    this.setState({ [component]: selection })
  }

  render() {
    return(<IngredientSelector updateSandwich={this.updateSandwich} />)
  }
}

class IngredientSelector extends React.Component {
  return(){
    <button value="Rye" onClick={() => this.updateSandwich("bread", "rye")} />
    <button value="Wheat" onClick={() => this.updateSandwich("bread", "wheat")} />
    <button value="Ham" onClick={() => this.updateSandwich("meat", "ham")} />
    <button value="Turkey" onClick={() => this.updateSandwich("meat", "turkey")} />
  }
}
class.Component{
此.state={
面包:“,
肉类:“,
素食者:“,
}
updateSandwich=(组件,选择)=>{
this.setState({[component]:selection})
}
render(){
返回()
}
}
类InCreditSelector扩展了React.Component{
返回(){
this.updateSandwich(“面包”、“黑麦”)}/>
这个.updateSandwich(“面包”、“小麦”)}/>
this.updateSandwich(“肉”、“火腿”)}/>
this.updateSandwich(“肉”、“火鸡”)}/>
}
}

感谢您的回复,第二个例子是我在创建一个示例来说明问题时的一个输入错误。我会解决的。我知道如何调用示例3中组件中的函数。我在每个触发的函数中都设置了一个控制台日志,只是“this”引用让我感到困惑。我喜欢示例方法,但是,我的示例再次被简化了。这并不总是只是设定状态,但我会牢记你的榜样。问题的关键在于
参考。
取决于上下文。在第三个示例中,这些函数被定义为闭包。
的值已绑定到您的
应用程序
组件。不管它们在哪里被调用,
只要你不重新绑定它们,这个
就不会改变。你能解释一下吗?我更新了第三个示例,更好地指出了这个问题。我假设我必须在objectproperty函数中绑定
这个
,因为直到那时它才被绑定。没有绑定,如示例一或示例二所示,
不会引用
应用程序
。箭头函数没有自己的
值。箭头函数中的
this
值始终从封闭范围继承。因此,在您的示例中,
这个
引用了组件的实例。对于普通闭包,您不会得到这种行为,相反,
这个
将是
窗口
未定义的
。感谢您的回答,第二个示例是一个输入错误,因为我正在创建一个示例来说明这个问题。我会解决的。我知道如何调用示例3中组件中的函数。我在每个触发的函数中都设置了一个控制台日志,只是“this”引用让我感到困惑。我喜欢示例方法,但是,我的示例再次被简化了。这并不总是只是设定状态,但我会牢记你的榜样。问题的关键在于
参考。
取决于上下文。在第三个示例中,这些函数被定义为闭包。
的值已绑定到您的
应用程序
组件。不管它们在哪里被调用,
只要你不重新绑定它们,这个
就不会改变。你能解释一下吗?我更新了第三个示例,更好地指出了这个问题。我假设我必须在objectproperty函数中绑定
这个
,因为直到那时它才被绑定。没有绑定,如示例一或示例二所示,
不会引用
应用程序
。箭头函数没有自己的
值。箭头函数中的
this
值始终从封闭范围继承。因此,在您的示例中,
这个
引用了组件的实例。对于普通闭包,您不会得到这种行为,相反,
将成为
窗口
未定义
this.props.handleStates.first()
class Sandwich extends React.Component {
  this.state = {
    bread: "",
    meat: "",
    veggie: "",
  }

  updateSandwich = (component, selection) => {
    this.setState({ [component]: selection })
  }

  render() {
    return(<IngredientSelector updateSandwich={this.updateSandwich} />)
  }
}

class IngredientSelector extends React.Component {
  return(){
    <button value="Rye" onClick={() => this.updateSandwich("bread", "rye")} />
    <button value="Wheat" onClick={() => this.updateSandwich("bread", "wheat")} />
    <button value="Ham" onClick={() => this.updateSandwich("meat", "ham")} />
    <button value="Turkey" onClick={() => this.updateSandwich("meat", "turkey")} />
  }
}