如何定位減速器中的數組中的元素/對象,我必須將該動作傳遞給?我試圖找出我對減速器採取的行動。或者如何改變影響對象的值number
。所以我渲染陣列,使TouchableOpacity視圖,其onPress林調用行動派遣如下:如何處理Reducer中的動作?
import React, { Component } from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { connect } from 'react-redux';
class NewData extends Component {
render(){
const renData = this.props.newData.map((data, idx) => {
return (
<View key={idx}>
<TouchableOpacity
onPress={() => this.props.incNum(data.id)}
//not sure how to toggel dispatch call above, for onPress
>
<Text style={styles.welcome}>{data.id} - {data.name} - {data.number}</Text>
</TouchableOpacity>
</View>
)
});
return(
<View>
{renData}
</View>
);
}
}
function mapStateToProps (state) {
return {
newData: state.newData
};
};
//I think I need to create a toggle here:
//Something like: this.props.newData.id ? incNum : decNum
function mapDispatchToProps (dispatch) {
return {
incNum: (id) => {
dispatch({
type: "INC_NUM",
payload: id
})
},
decNum: (id) => {
dispatch({
type: "DEC_NUM",
payload: id
})
}
};
};
export default connect(mapStateToProps, mapDispatchToProps)(NewData)
我減速機:
const initialState = [
{
id: '01',
name: 'Name One',
number: 11
},
{
id: '02',
name: 'Name Two',
number: 22
},
{
id: '03',
name: 'Name Three',
number: 33
}
]
export default function newData (state = initialState, action) {
switch (action.type) {
case "INC_NUM":
return {
...state,
number: this.state.number ++ // <== need help here.
}
case "DEC_NUM":
return {
...state,
number: this.state.number --
}
default:
return state
}
}
我認爲<TouchableOpacity onPress={() => this.props.incNum(data.id)}
將結合id
改變。並通過payload: id
將ID傳遞給reducer。但是,如何更新減速器中的值?現在我可以不用切換。我想了解如何在價格中傳遞價值並進行相應更新。
謝謝。
EDIT1:
所以我現在減速器是:
const initialState = {
1: {
id: '01',
name: 'Name One',
number: 11
},
2: {
id: '02',
name: 'Name Two',
number: 22
}
}
export default function newData (state = initialState, action) {
switch (action.type) {
case "INC_NUM":
return {
...state,
[action.payload]: {
...state[action.payload],
number: state[action.payload].number++ <== Line 58 in ERROR screenshot
}
case "DEC_NUM":
return {
...state,
[action.payload]: {
...state[action.payload],
number: state[action.payload].number--
}
default:
return state
}
}
而且我呈現的方式是:
class NewData extends Component {
render(){
const renData = Object.keys(this.props.newData).map((key, idx) => {
let data = this.props.newData[key]
return (
<View key={idx}>
<TouchableOpacity
onPress={() => this.props.updateNum(data.id)}
>
<Text style={styles.welcome}>{data.id} - {data.name} - {data.number}</Text>
</TouchableOpacity>
</View>
)
});
return(
<View>
{renData}
</View>
)
}
}
function mapDispatchToProps (dispatch) {
return {
updateNum: (id) => {
INC_NUM({
type: "INC_NUM",
payload: id
})
}
};
};
一切都在減速是如預期般出現。但是,當我點擊和動作被調用時,我得到一個錯誤:
您需要在減速器中進行一些過濾。你目前的reducer試圖更新state.number,這個不存在。 另外你的initialState應該是一個不是數組的對象。我通常用這樣的id映射我的狀態: { 1:{id:1,name:'number one'...}, 2:{id:2,...}, .... } – MichaelWClark
謝謝。請你指點一下我可以閱讀/參考的東西。我是React和Redux的新手,並努力學習。 – Somename
另外,你應該使用action.payload過濾你的狀態對象。這就是你的'id'進來的地方。希望這會有所幫助。 – MichaelWClark