2017-01-01 87 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, 
    }; 
} 
相关问题