我的代码块如下:
class Buerger extends React.Component {
constructor() {
super();
this.gehaltHandler = this.gehaltChange.bind(this)
}
render() {
return(
<div style={{textAlign:"right", marginTop:'-103', marginRight:'900px'}}>
<Anzeige stand = {"Bargeld: " + this.props.stand}/>
<Arbeit gehalt = {this.props.gehalt} gehaltChange = {this.gehaltChange} arbeitenFunc = {this.props.arbeitenFunc}/>
</div>
)
}
gehaltChange(inputWert) {
const test = this.props.gehalt
alert(test)
}
因此,我从父类传递了 Prop “gehalt”,您可以看到我再次将其传递给名为“Arbeit”的子类的子类。
Funktion gehaltChange 正在使用警报('click')进行检查,但使用此代码我得到“无法读取未定义的属性'props'”。
将 gehalt 传递给“Arbeit”也有效,因为我已经将其作为按钮名称进行了测试,并且它显示了正确的值,没有问题。
我不明白。有人看到我做错了什么吗?是不是不能把之前传过的 Prop 同时使用呢?我将不胜感激任何帮助。
最佳答案
因为您需要向构造函数传递 props
并使用它们调用 super
。
class Buerger extends React.Component {
constructor(props) {
super(props);
this.gehaltHandler = this.gehaltChange.bind(this)
}
关于javascript - 为什么我不能在这个方法中调用 props ? ( react ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45636025/