在我的应用中,我可以使用以下功能从数组中获取值,距离和位置名称。但是,我无法使用mapDispatchToProps在我的redux存储中分派从它们获得的值, 例如
handleNavigation() {
this.props.navigation.navigate('LocationLists');
this.props.totalDistanceChange(this.totalDistance()).
}
<Button
onPress={this.handleNavigation.bind(this)}
/>
const mapDispatchToProps = (dispatch) => ({
totalDistanceChange: totalDistance=> {
dispatch(totalDistanceChange(totalDistance));
}
});
在现有状态转换期间,我一直无法获取更新。 下面只是我要使其保持尽可能简单的功能,请在适当的地方正确无误。
totalDistance = () => {
const { selectedItemObjects } = this.state;
const total = selectedItemObjects.reduce((result, { Distance }) => result += Distance, 0);
return total.toFixed(1);
}
totalValue = () => {
const { selectedItemObjects } = this.state;
const total = selectedItemObjects.reduce((result, { Value }) => result += Value, 0);
return total;
}
renderLocationText = () => {
const { selectedItemObjects } = this.state;
return selectedItemObjects.length ?
`${selectedItemObjects.map((item, i) => {
let label = `${item.name}, `;
if (i === selectedItemObjects.length - 2) label = `${item.name} and `;
if (i === selectedItemObjects.length - 1) label = `${item.name}.`;
return label;
}).join('')}`
:
null;
}
我的问题是如何将获得的值传递给我的redux存储区
答案 0 :(得分:-1)
const mapDispatchToProps = (dispatch) => ({
// HERE YOU HAVE GIVEN THE SAME NAME 'totalDistanceChange'
totalDistanceChange: totalDistance=> {
dispatch(totalDistanceChange(totalDistance));
}
});
喜欢这样做。
this.props.onTotalDistanceChange(this.totalDistance());
const mapDispatchToProps = (dispatch) => ({
onTotalDistanceChange: totalDistance=> {
dispatch(totalDistanceChange(totalDistance));
}
});
答案 1 :(得分:-1)
通过将其转换为字符串进行了修复
handleNavigation() {
this.props.navigation.navigate('LocationLists');
this.props.totalDistanceChange(this.totalDistance().toString()).
}