2017-01-01 4 views
0

add buttonを使用して動的リストを生成しています。動的リストを生成できますが、liにもdeleteボタンがあります。削除ボタンを押したときにアイテムを削除したいのですが、actionも同様です.Firstly私は、動的にイベントをクリックしバインドする方法を知っているDON''Tユーザープレスがここでボタン反応の削除ボタンを使ってリストから項目を削除する方法は?

を削除するとlist.Iがエラーを取得しています生成された私のコードは https://plnkr.co/edit/JEG6o4JCBIQWAt2A4S3r?p=preview です//コードをここに

const { createStore, bindActionCreators, applyMiddleware,combineReducers } = Redux; 
const { Provider, connect } = ReactRedux; 
const thunk = window.ReduxThunk.default; 


const first_redux =function(state =[] ,action){ 
    switch (action.type){ 
    case 'ADD_ITEM': 
    return [ 
     ...state, 
     action.payload 
     ]; 
     case 'DELETE_ITEM': 
     var index = state.indexOf(action.payload); 
    return state.slice(index,1); 
    default : 
    return state 

    } 
} 

const actions ={ 
addItem :function(item){ 
    return { 
    type :"ADD_ITEM", 
    payload :item 
    } 
} , 
deleteItem :function(item){ 
    return { 
    type :"DELETE_ITEM", 
    payload :item 
    } 
} 

} 
var combindReducer = combineReducers({ 
    item:first_redux 
}) 

const store = createStore(combindReducer); 

class First extends React.Component { 
    constructor (props){ 
    super(props); 
    this.state = { 
     names :[], 
     username :'' 
    } 
    this.changeEv =this.changeEv.bind(this); 
    this.addName =this.addName.bind(this); 
    this.handle =this.handle.bind(this); 

    } 
    changeEv(e){ 
    this.setState({ 
     username : e.target.value 
    }) 
    } 
    addName(){ 
    if(this.state.username !== ''){ 
    this.props.add(this.state.username) 
    this.setState({ 
     username : '' 
    }) 
    } 
    } 
    handle(){ 
    alert('---') 
    } 

    render() { 
    const data =[{"name":"test1"},{"name":"test2"}]; 
    var listItems = this.props.names.map(function(d, idx){ 
     return (<li key={idx}><span>{d}</span><button onClick={this.handle}>delete</button></li>) 
    }) 
    return (
     <div> 
     <input type="text" onChange ={this.changeEv} value={this.state.username}/> 
     <button onClick={this.addName}>add</button> 
     {listItems} 
     </div> 
    ); 
    } 
} 

function mapStateToProps(state){ 
    console.log('================='); 
    console.log(state); 
    return { 
     names: state.item, 

    }; 
    console.log(this.state); 

} 

function mapDispatchToProps(dispatch){ 
    return { 
    add:bindActionCreators(actions.addItem,dispatch) 
    } 
} 
const Appcontainer =connect(mapStateToProps,mapDispatchToProps)(First) 

ReactDOM.render(
    <Provider store ={store}> 
    <Appcontainer/> 
    </Provider> 
    ,document.getElementById('root')); 
+0

plunkurは動作していません。私がaddを押すと、リストが生成されるのを見ることができません – Swapnil

答えて

1

を行きます問題:あなたはthis.props.names.mapの内側にアクセスしようとしているこのは、コンポーネントのこのではないので

あなたは削除ボタンでエラーが発生します。

ソリューション

var listItems = this.props.names.map((d, idx) => { 
    return (<li key={idx}><span>{d}</span><button onClick={this.handle}>delete</button></li>) 
}) 

あなたはES6を使用しているので、あなたはレキシカルスコープを提供arrow functionsを使用することができます。あなたのコードを更新しました。それを確認してくださいhere

+0

どのように要素を削除するのですか? – user944513

+0

私は削除機能で大群を更新しました。削除操作をmapDispatchToProps内のコンポーネントにpropとして提供するだけです(要素の追加の場合と同じです)。 – Swapnil

+0

私は助けてくれてありがとう – user944513

関連する問題