2016-12-19 4 views
0

私はReact.jsを使用していますが、Todoアプリを作成しています。なぜ私のチェックボックスの中でonClickが未定義ですか?

内容はこのコードにほぼ似ています。私は、チェックボックスにonChange機能を見つけることができないのはなぜ

class TodoValue extends React.Component{ 

    _onChange(){ 
    console.log("atacked"); 
    } 

    render(){ 
    return(
     <List> 
      { 
      this.props.todos.map(function(todo, i){ 
       return <ListItem key={i} primaryText={todo.item} rightIcon={<ActionInfo />} leftCheckbox={<Checkbox onClick={this._onChange.bind(this)} />} /> 
      }) 
      } 
     </List> 
    ); 
    } 
} 

更新情報の入力とボタンは省略されています。

+2

可能性のある重複した[地図内の匿名関数内のメソッドが定義されていない]を使用することができます(http://stackoverflow.com/questions/40076995/method-inside-anonymous-function -inside-map-is-undefined) – Li357

+0

@yuki nagahamaはあなたのために働いていますか? –

答えて

0

あなたのコンストラクタに宣言を追加します。

class TodoValue extends React.Component{ 
 
    constructor() { 
 
    super() 
 
    this._onChange = this._onChange.bind(this) 
 
    } 
 
    _onChange(){ 
 
    console.log("atacked"); 
 
    } 
 

 
    render(){ 
 
    return(
 
     <List> 
 
      { 
 
      this.props.todos.map(function(todo, i){ 
 
       return <ListItem key={i} primaryText={todo.item} rightIcon={<ActionInfo />} leftCheckbox={<Checkbox onClick={this._onChange.bind(this)} />} /> 
 
      }) 
 
      } 
 
     </List> 
 
    ); 
 
    } 
 
}

が追加constructorに注意してください。

詳しくはhereをご覧ください。

+0

'constructor'に' super'を追加する必要もあります –

+0

私が知る限り、 'constructor'の中で' props'を使う必要がある場合にのみ 'super'が必要です –

+0

http://cheng.logdown.com/投稿/ 2016/03/26/683329 –

0

1つのオプションは、別のアプローチは、のconstructor方法で構築を反応させるのに利用するだろう

Checkbox onClick={()=> this._onChange.bind(this)} 

es6矢印機能を使用することです。ここでは、_onChangeメソッドをコンポーネントにバインドする必要があります。

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

次にあなたが

Checkbox onClick={this._onChange} 
+0

これはこの方法で解決されました!ありがとうございました! –

関連する問題