【发布时间】:2019-10-15 22:50:30
【问题描述】:
错误:TypeError:_this.props.handleChange 不是函数
我有一个 React 组件,它有一个用于另一个视图组件的 prop 方法。
问题是我的其他视图组件需要使用我的 Child 组件但是它会导致错误,因为它需要使用 prop 方法。
class NeedsPropMethod extends Component {
handleChange = value => {
// receives the value
}
return (
<Child toy="car" color="blue" handleChange={this.handleChange}>
)
}
class DoesntNeedPropMethod extends Component {
return (
<Child toy="car" color="blue"}>
)
}
class Child extends Component {
return (
this.props.toy; // "car"
this.props.color; // "blue"
this.props.handleChange(value) // I do not need this but it gives me the error above
);
}
【问题讨论】:
-
你没有通过它,如果你不需要它删除它。
-
您上面的
Child类有语法错误。 真正应该是什么样子? -
你可以添加一个布尔检查,如果为真,返回
handlechange,否则不要运行 -
我希望我可以,但是 prop 方法正在我的其他视图组件中使用,所以我试图找出一种使其可重用或类似的方法。将在我的示例中添加更多代码。
标签: javascript node.js reactjs ecmascript-6 single-page-application