2017-01-03 6 views
0

に「この」上のフィルタを使用すると、これはReactjs

class ComponentStart extends Component { 
    constructor() 
    { 
     super(); 
     this.count = 0; 
     this.state = {}; 
     this.repeats = []; 
    } 

    delete_this(index) 
    { 
     console.log("I am deleting"); 
     console.log(index); 
     this.repeats = this.repeats.filter((item) => item != index); 
     this.setState({ repeats: this.repeats }); 
    } 
    componentWillMount() 
    { 
     for (let i = 0; i < this.props.number; i++) 
     { 
      this.repeats.push(<StartMultiple key={this.count} id={this.count} delete_this={this.delete_this.bind(this)}/>);    
      this.count++; 
     }  
     this.setState({ repeats: this.repeats}); 
    } 

    componentHasMounted() 
    { 
    } 

    render() 
    { 
     return(
      <div> 
       {this.state.repeats} 

       <button onClick={this.add_repeat.bind(this, event)}>clickable</button> 
      </div> 
     ); 
    } 

私の親クラスであり、これは子クラスである:

export default class StartMultiple extends Component { 
    constructor() 
    { 
     super(); 
     this.options = [ 1, 2, 3, 4, 5]; 
     this.temp_option = []; 
     this.delete_me = this.delete_me.bind(this); 
     this.buttons = [<button key="0" onClick={this.delete_me}/>,<button key="1" onClick={this.delete_me}/>]; 
     this.state = { buttons: this.buttons }; 
    } 

    ComponentDidMount() 
    { 
     $.ajax({ 
      url: "src/php/search.php?type=search", 
      dataType: "json", 
      success: (data) => 
      { 
       console.log(data); 
      } 
     }); 
    } 

    delete_this(value) 
    { 
     console.log(value); 
     this.props.delete_this(value); 
     return; 
    } 

    render() 
    { 
     console.log(this.props); 
     return(
      <div> 
       <input id={"input" + this.props.id} type="text"/> 
       <select> 
        {this.options.map(this.toOptions)} 
       </select> 
       <div> 
        I am some text 
       </div> 
       <div> 
        <button onClick={this.hide_info.bind(this)}>hide previous</button> 
        <button onClick={this.delete_this.bind(this, this)} ref={ (input) => { this.button = input; } } value={"hi"}>delete</button> 
       </div> 

       {this.buttons} 
      </div> 
     ); 
    } 
} 

だから何が起こることは、私はStartMultipleのボタンをクリックしたときにということです<button onClick={this.delete_this.bind(this, this)} ref={ (input) => { this.button = input; } } value={"hi"}>delete</button>親のdelete_this機能をトリガーします(これは機能します)。

フィルタリングの部分を除いて、基本的にすべてが正常に動作しています。私はそれが正しく構成要素を渡すだとしても、それはフィルタリングいない理由はわからないあなたは、あなたが作成する要素に反応比較している

+1

このコードには多くの問題があります。 –

答えて

2

this.repeats.push(<StartMultiple ... delete_this={this.delete_this.bind(this)}/>);    

インスタンスであるthisになりますindexComponentStart

this.repeats = this.repeats.filter((item) => item != index); 

thisのあなたが期待するものではありませんが、チェックインスタンスに反応することにより、配列内の要素を見ていません。配列内のIDまたはインデックスを比較するなど、データのみを含む配列から項目を削除します。

+0

しかし、私は子の値を親関数に渡していますか?だから代わりにStartMultipleの値を渡しませんか?私は反応にあまり慣れていないので、バニラのjavascriptのロジックを使用しています。ここで、ボタンの 'this'をフィルタを介して削除する関数に渡します。\ –

+0

新しい 'this' isn forループ内に作成されていません。 http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work –

+0

私は 'console.log(index instanceof Start);'を実行しますが、 'false 'ですが、' console.log (インデックスinstanceof StartMultiple);それは私が期待しているように機能していることを意味する、真実です... –