2017-01-01 85 views
0

我試圖使用反應。這裏是我的代碼 https://plnkr.co/edit/JEG6o4JCBIQWAt2A4S3r?p=preview如何使用反應在列表中添加項目?

我想在列表中添加輸入字段文本值當用戶按下按鈕添加。在我的演示中,我就點擊我想有一個add button列表中添加的項在列表中添加項目。 你能告訴我我在做什麼錯嗎?

// Code goes here 

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 action.payload 
    default : 
    return state 

    } 
} 

const actions ={ 
addItem :function(item){ 
    return { 
    type :"ADD_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); 
    } 
    changeEv(e){ 
    this.setState({ 
     username : e.target.value 
    }) 
    } 
    addName(){ 
    // this.state.names.push(this.state.username); 
    console.log(this.state); 
    this.props.add(this.state.username) 
    } 

    render() { 
    const data =[{"name":"test1"},{"name":"test2"}]; 
    var listItems = this.state.names.map(function(d, idx){ 
     return (<li key={idx}>{d.name}</li>) 
    }) 
    return (
     <div> 
     <input type="text" onChange ={this.changeEv}/> 
     <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

我當您使用終極版做在你的代碼一對夫婦的變化,在這種情況下,存儲是在組件的外面和mapDispatchToProps是用組件的道具映射全局狀態。

//代碼放在這裏

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

const initialState = [] 

const first_redux =function(state = initialState ,action){ 
    switch (action.type){ 
    case 'ADD_ITEM': 
    let newState = action.payload; 
    return [...state,newState]; 
    default : 
    return state 

    } 
} 

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

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

const store = createStore(combindReducer); 

class First extends React.Component { 
    constructor (props){ 
    super(props); 
    this.state = { 
     username :'' 
    } 
    this.changeEv =this.changeEv.bind(this); 
    this.addName =this.addName.bind(this); 
    } 
    changeEv(e){ 
    this.setState({ 
     username : e.target.value 
    }) 
    } 
    addName(){ 
    // this.state.names.push(this.state.username); 
    console.log(this.state); 
    this.props.add(this.state.username) 
    } 

    render() { 
    const data =[{"name":"test1"},{"name":"test2"}]; 
    var listItems = this.props.names.map(function(d, idx){ 
     return (<li key={idx}>{d}</li>) 
    }) 
    return (
     <div> 
     <input type="text" onChange ={this.changeEv}/> 
     <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')); 

https://plnkr.co/edit/e6oJXempwk0e5GciOSSB?p=previewp

0

你在減速什麼樣的回報基本上是你的應用程序的狀態,但現在你只是返回你的負載,從而你的狀態,在這種情況下,僅僅是一個單一的項目。你想要的是,你的狀態是一個項目列表,因此你的狀態應該創建一個數組,而不僅僅是返回有效載荷。注意reducer中的狀態應該是不可變的,所以我們不能簡單地將新的有效負載推到最後一個狀態。

這裏是你如何能實現這樣一個例子:

const first_redux = function(state = [] ,action){ 
    switch (action.type){ 
    case 'ADD_ITEM': 
     return [ 
     ...state, 
     action.payload 
     ]; 
    default : 
     return state 
    } 
} 

var combindReducer = combineReducers({ 
    items: first_redux 
}) 

function mapStateToProps(state){ 
    console.log('================='); 
    console.log(state); 
    return { 
    names: state.items, 
    }; 
} 
相關問題