2017-01-11 6 views
-1

JSXでes6 arrow関数を使用すると、別のコンポーネントからpropsが来る場合、このようなargsを渡すことができます。反応してes6バインドを使用するときにargを渡す

class MyComponent extends Component({ 
    myFunc(param){ 
    console.log(param); 
    } 
    render(
    return(
     <button onClick="(param)=>myFunc(param)"></button> 
    ) 
) 
}) 

しかし、私はあなたがこの種を使用する場合

class MyComponent extends Component({ 
    constructor(){ 
    this.myFunc = this.myFunc.bind(this); 
    } 
    myFunc(){ 

    } 
    render(
    return(
     <button onClick={this.myFunc}></button> 
    ) 
) 
}) 

は、どのように私はparamsが何を渡すことができますか?

+0

これはどのような用途に使用しますか?子オブジェクトの削除のようなものですか? – illusionist

+0

@illusionist子供のコンポーネントに何かを伝えたいかもしれません。 –

+0

'onClick = {this.myFunc.bind(this)}'または 'onClick = {e => this.myFunc(e)}' – naomik

答えて

0

あなたはこの作品をすべき

constructor(){ 
    this.myFunc = myFunc.bind(this); 
} 

このラインでミスを犯しています

constructor(){ 
    this.myFunc = this.myFunc.bind(this); 
} 

myFunc(props){ 
    console.log(props); 
} 
0

あなたが最初のもののように直接渡すことはできません。しかし、e.targetを使って別の方法でアクセスすることができます。しかし、私はあなたがこれを好きなシナリオではわからない。

class MyComponent extends Component({ 
    constructor(){ 
     this.myFunc = this.myFunc.bind(this); 
    } 
    myFunc(e){ 
    console.log(e.target.arg); 
    } 
    render(){ 
     return(
     <button arg={yourargument} onClick={this.myFunc}></button> 
    ) 
    } 
}) 
関連する問題