2017-08-11 67 views
2

我的代码块如下:为什么我不能在这种方法中调用道具? (反应)

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) 
    } 

所以我通过道具“gehalt”从父类,你可以看到我再次把它传递给所谓的“Arbeit子类的子类“。

Funktion gehaltChange正在使用警报('click')进行检查,但通过此代码,我得到了“无法读取属性”的未定义属性。

gehalt到“Arbeit”的传递也在工作,因为我测试它作为一个按钮名称,它显示正确的值没有问题。

我不明白。有人看到我做错了什么?难道我不能通过以前通过的道具并同时使用它们吗?我会感谢任何帮助。

回答

0

因为你改了个名字

this.gehaltHandler = this.gehaltChange.bind(this) 

VS

gehaltChange={this.gehaltChange} 

,所以你将其绑定到一个新的名称,然后通过原来的绑定功能。

gehaltChange={this.gehaltHandler}this.gehaltChange = this.gehaltChange.bind(this)

+0

对不起,谢谢funktion正在工作,但我实际上没有通过绑定的功能作为一个名称更改解决它的道具。 => gehaltChange = {this.gehaltHandler} 尽快接受答案。 – OhLongJohnson

2

因为您需要通过构造函数props并且还请拨打super

class Buerger extends React.Component { 
    constructor(props) { 
    super(props); 
    this.gehaltHandler = this.gehaltChange.bind(this) 
    } 
+0

不认为在有任何影响的props参数这个问题。 –

0

我想你是不是以正确的方式结合。 替换此行:

this.gehaltHandler = this.gehaltChange.bind(this); 

对于这一个:

this.gehaltChange = this.gehaltChange.bind(this); 

又通道具如上面有人回答。

0

只要改变这一点:

constructor() { 
    super(); 
    this.gehaltHandler = this.gehaltChange.bind(this) 
    } 

这样:

constructor(props) { 
    super(props); 
    this.gehaltHandler = this.gehaltChange.bind(this); 
    } 

你错过了在这两个constructorsuper方法:)

相关问题