【发布时间】:2018-02-03 23:42:22
【问题描述】:
如何定位减速器中数组中的哪个元素/对象,我必须将动作传递给?我试图弄清楚我对减速器采取了什么行动。或者我如何更改影响对象中的number 值。所以我在一个带有 TouchableOpacity 的视图中渲染数组,它 onPress 我调用动作调度如下:
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 中的所有内容都按预期显示。但是当我单击并调用该操作时,出现错误:
【问题讨论】:
-
你需要在你的 reducer 中做一些过滤。您当前的 reducer 正在尝试更新不存在的 state.number。此外,您的 initialState 应该是一个对象而不是数组。我通常像这样按 id 映射我的状态: { 1: {id: 1, name: 'number one'...}, 2: {id:2,... }, .... }
-
谢谢。请您指出一些我可以阅读/参考的内容。我是 React 和 Redux 的新手,正在努力学习。
-
另外,您应该使用您的 action.payload 过滤您的状态对象。这就是您的“身份”的来源。希望这会有所帮助。
-
大约一年前我不得不同时学习这两种方法。在尝试结合学习两者之前,我强烈建议在几个教程中分别学习 React 和 Redux。 redux.js.org/docs/basics/ExampleTodoList.html 是 redux 的一个很好的开始。如有任何问题,请随时 PM 我。另一个资源:egghead.io/courses/getting-started-with-redux
-
谢谢 .. 自过去 2 个月以来,我已经多次阅读提到的两个教程。我想我的问题更多是与 JS 相关的,我无法弄清楚。
标签: reactjs react-native redux react-redux