【发布时间】:2017-03-26 10:15:40
【问题描述】:
您好,我有这个对象,并将对其进行一些数学/统计运算。现在我要提问:
如何访问它?例如我想访问 numbers[0]['B1'] 当我执行 {this.props.numbers[0]['B1']} 时,我得到:无法读取未定义的属性 B1。
如果我想对这些数字进行一些计算,我会将它们放在哪里?根据我对 react redux 的有限经验,我知道我不应该在 reducer 中做类似的事情,对吗?我会创建更多的动作(动作创建者),例如。获取平均 B1 数字,或对数字进行任何统计操作等。是否建议对此类任务使用“重新选择”?
numReducer
import { LIST_NUMBERS, PICK_NUMBER, GET_DATA } from '../actions/actionTypes';
export default (state = [], action = {}) => {
switch (action.type) {
case LIST_NUMBERS:
return action.payload || [];
case PICK_NUMBER:
return action.payload;
case GET_DATA:
return action.payload;
default:
return state;
}
};
行动:
import { LIST_NUMBERS, PICK_NUMBER, GET_DATA } from './actionTypes';
import dataSet from '../data.json';
export const listNumbers = () => {
const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
return {
type: LIST_NUMBERS,
payload: nums
};
};
export const getData = () => {
return {
type: GET_DATA,
payload: dataSet
};
};
export const pickNumber = (num) => {
return {
type: PICK_NUMBER,
payload: num
};
};
数据.json
[
{
"DrawDate": "22-Mar-17",
"B1": 12,
"B2": 6,
"B3": 11,
"B4": 31,
"B5": 27,
"B6": 19,
"BB": 42,
"BS": 1,
"DrawNumber": 2217
},
{
"DrawDate": "18-Mar-17",
"B1": 26,
"B2": 37,
"B3": 8,
"B4": 3,
"B5": 19,
"B6": 41,
"BB": 43,
"BS": 3,
"DrawNumber": 2216
},
....
主容器
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { listNumbers, pickNumber, getData } from '../actions/numberActions';
import Home from '../components/Home';
const mapStateToProps = state => ({
numbers: state.numbers
});
const mapDispatchToProps = dispatch => (
bindActionCreators({
listNumbers,
pickNumber,
getData
}, dispatch)
);
export default connect(
mapStateToProps,
mapDispatchToProps
)(Home);
主页组件:
import React, { Component } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
export default class Home extends Component {
static navigationOptions = {
title: 'Home Screen',
};
componentDidMount() {
this.props.getData();
}
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Text>####################</Text>
<Text>Intro Screen</Text>
<Text>Number: {this.props.numbers[0]['B1']}</Text>
</View>
);
}
}
编辑/添加:
根据下面的建议,我已将生命周期方法更改为 ComponentWillMount 并添加了检查 this.props.numbers 是否已加载。
import React, { Component } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
export default class Home extends Component {
static navigationOptions = {
title: 'Home Screen',
};
componentWillMount() {
this.props.getData();
}
render() {
if (!this.props.numbers) {
console.log('not yet loaded'); // or a spinner?
}
const { navigate } = this.props.navigation;
return (
<View>
<Text>####################</Text>
<Text>Intro Screen</Text>
<Text>Number: {this.props.numbers[0]['B1']}</Text>
</View>
);
}
}
我仍然收到相同的错误:无法读取未定义的属性“B1”。此外,控制台没有记录“尚未加载”,这表明数字对象存在 - 我只是在访问它时出错。
EDIT2:
import React, { Component } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
export default class Home extends Component {
static navigationOptions = {
title: 'Home Screen',
};
componentWillMount() {
this.props.getData();
}
listNums() {
return this.props.numbers.map((num) => num['B1']);
}
listSingleNum() {
return this.props.numbers[0]['B1'];
}
render() {
if (!this.props.numbers) {
console.log('not yet loaded'); // or a spinner?
} else {
console.log(this.listNums());
}
const { navigate } = this.props.navigation;
return (
<View>
<Text>####################</Text>
<Text>Intro Screen</Text>
<Text>Number: {this.listNums()}</Text>
</View>
);
}
}
所以 listNums() 可以很好地显示每个元素的 B1,但是如果我尝试像在 listSingleNum 中那样访问单个 B1 元素,它会抛出前面提到的错误:ExceptionsManager.js:63Cannot read property 'B1' of undefined。
【问题讨论】:
-
console.log(this.props.numbers) 是什么样的?也许它是一个空数组? (´![] === false´)
-
我已经更新了问题。回答您的问题:它在 console.log 中显示了两次。第一次是 [ ],第二次是应有的整个数组。
标签: json react-native react-redux