class Parent extends Component {
    constructor(props) {
    	super(props);
    	this.state = { "id": "1" };
    }
    
    setIdFromChildA = (id) => this.setState({ "id": id });
    setIdFromChildB = (id) => this.setState({ "id": id });
    
    render() {
    	return (
    	    <ChildA id={this.state.id} setId={this.setIdFromChildA} />
    	    <ChildB id={this.state.id} setId={this.setIdFromChildB} />
        );
    }
}
class ChildA extends Component {
	constructor(props) {
    	super(props);
    	this.state = {...};
    }
    
    componentDidMount() {
    	this.foo()
    }
    
    foo() {
    	this.props.setId("2");
    }
}
class ChildB extends Component {
    constructor(props) {
    	super(props);
    	this.state = {...};
    }
    
    handleClick = (e) => {this.props.setId("2")};
    
    render() {
    	return (
            <button type="button" onClick={this.handleClick} />
    	);
}
现在的问题是 ChildA 调用 setId 没有问题但是 ChildB 在调用时会报错TypeError: _this.props.setId is not a function。A 和 B 的区别在于一个是在componentDidMount()中执行另一个是 onClick 事件触发,是因为这个原因吗?
|      1swirling      2018-02-02 16:00:02 +08:00 via iPhone 我觉得应该是 a 报错 b 不报错才对吧 | 
|  |      250480350      2018-02-02 16:06:59 +08:00 this.handleClick = this.handleClick.bind(this); | 
|  |      3brickyang      2018-02-02 17:17:37 +08:00 via iPhone 你在 childA 的 constructor() 里加上 this.foo = this.foo.bind(this) 试试 | 
|  |      4brickyang      2018-02-02 17:19:15 +08:00 via iPhone 噢噢,眼花了,在 childB 的 constructor 里给 handleClick bind this 试试 | 
|      5Oathbinder OP | 
|  |      6Mullvinn      2018-02-02 17:54:58 +08:00 跑了一下贴出来的代码,没发现报错,看看是不是其他哪里有问题 | 
|      7yacolinqi      2018-02-02 21:28:03 +08:00 via Android 断点调试一下呗,或者在 handleClick 打印一下看看呗 |