简体   繁体   English

在React JS中内部未定义方法/函数

[英]method/function undefined inside map in react js

contructor:
this.getMeSomething = this.getMeSomething.bind(this)//////


getMeSomething(abc, xyz, event){}


class xyz extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (
            <div style={{ ...}}>
                    {
                    this.props.cars.map((car) => {
                        return <div><span className="yhn"><label><input type="checkbox" onChange={this.getMeSomething(this, this.props.color, car)} /><span></span></label>{car}</span></div>
                        })
                    }
            </div>
        );
    }
}

Error at 错误于

<div><span className="yhn"><label><input type="checkbox" onChange={this.getMeSomething(this, this.props.color, car)} /><span></span></label>{car}</span></div>

execution. 执行。

this is showing as window and getMeSomething is coming as undefined. 这显示为窗口,而getMeSomething也未定义。 Not sure what is wrong in the above. 不知道上面有什么问题。

getMeSomething needs to be defined inside the class to use it with this . 需要在类内部定义getMeSomething ,以便与this结合使用。

    class xyz extends React.Component {
  constructor(props) {
      super(props);
      this.getMeSomething = this.getMeSomething.bind(this)
  }

  getMeSomething(event){}

  render() {
    return (
      <div style={{ ...}}>
            {
            this.props.cars.map((car) => {
              return (
                <div>
                  <span className="yhn">
                    <label><input type="checkbox" onChange={this.getMeSomething} />
                    </label>
                    {car}
                  </span>
                </div>
                )
              })
            }
      </div>
    );
  }
}

If you want getMeSomething to be outside the class, then another way is to pass it as props . 如果您希望getMeSomething在类之外,那么另一种方法是将其作为props传递。 Avoid attaching it to the global window object. 避免将其附加到全局window对象。

Also this is implicitly available within getMeSomething and so are props . 而且, thisgetMeSomething隐式可用,而props也隐式可用。

I am not entirely sure what is going on at the top of the code sample. 我不完全确定代码示例顶部发生了什么。 I am reading it as the method getMeSomething is defined outside of the class xyz. 我正在阅读它,因为方法getMeSomething在类xyz之外定义。 Therefore, it would be undefined for class xyz, which the this keyword is pointing to. 因此,此关键字指向的类xyz将是未定义的。 Comment, or edit the code sample if I am reading it wrong. 如果我读错了,请注释或编辑代码示例。 The solution is to move it within the class, or to not refer to it using the this keyword. 解决方案是在类中移动它,或者不使用this关键字引用它。

class xyz extends React.Component {
    constructor(props) {
        super(props);
    }
    // Move here
    getMeSomething(abc, xyz, event){}

    render() {
        return (
            <div style={{ ...}}>
                    {
                        // Value of this is the class xyz
                    this.props.cars.map((car) => {
                        // Lambda functions keeps value of this as same as outer scope, so still class xyz.
                        return <div><span className="yhn"><label><input type="checkbox" onChange={this.getMeSomething(this, this.props.color, car)} /><span></span></label>{car}</span></div>
                        })
                    }
            </div>
        );
    }
}

EDIT: Okay, here is a refactored solution that avoids having to use bind and simplifies your render template logic. 编辑:好的,这是一个重构的解决方案,避免了必须使用bind并简化了渲染模板逻辑。 It appears to be working in this JSFiddle . 它似乎在此JSFiddle中工作。

class CarsComponent extends React.Component {

    getMeSomething(car, isChecked) {
        console.log(isChecked); // True or False
        console.log(car); // Car that was clicked - ex: 'Honda' 
        console.log(this.props.colors); // [ 'blue', 'green', 'yellow' ]
    }

    renderCars(car) {
        return this.props.cars.map((car, i) => {
            return (
                <div key={i}>
                    <span className="yhn">
                        <label>
                            <input type="checkbox" onChange={(e) => this.getMeSomething(car, e.target.checked)} />
                            <span>
                            </span>
                        </label>
                        {car}
                    </span>
                </div>
            )
        });
    }

    render() {
        return (
            <div>
                { this.renderCars() }
            </div>
        );
    }
}

// Test prop variables, you will car your own notion of what these are.
const cars = [ 'Honda', 'Toyota' ];
const colors = [ 'blue', 'green', 'yellow' ];

ReactDOM.render(
    <CarsComponent cars={cars} colors={colors}/>,
    document.getElementById('container')
);

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

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