我仍在学习本机react,我正在尝试从 API 检索数据并将其作为自定义单选按钮返回,但是如果我确实调用了 API,则会收到此错误:
null 不是一个对象(评估'this.props.activities.map')
{this.props.activities.map((val, index) => {
let { key, type, placeholder } = val;
if (type === "selection") {
var buttons = [];
placeholder.forEach((e, index) => {
var selectedButton = img.findButton(e, true);
var normalButton = img.findButton(e);
buttons.push(
<RadioButton
key={index}
value={e}
element={<Image source={selectedButton} />}
selectedElement={<Image source={normalButton} />}
onPress={() => this.changeSelection(key, e)}
selected={this.state[key]["value"]}
/>
);
});
var rows = [],
columns = [];
var i = 0;
buttons.forEach((e, index) => {
rows.push(e);
i++;
if (i === 2 || index === buttons.length - 1) {
//max buttons per row
i = 0;
columns.push(
<View key={index} style={{ flex: 1, flexDirection: "row" }}>
{rows}
</View>
);
rows = [];
i = 0;
}
});
return (
<View key={key} style={{ flex: 1, margin: normalize(20) }}>
{columns}
</View>
);
}
})}
'this.props.activites' 来自于此
let initialState = {
activities: null,
};
export default function mainReducer(state = initialState, action) {
switch (action.type) {
case t.RECEIVE_ACT:
return Object.assign({}, state, { actReceived: true, activities: action.activities });
case t.EMPTY_ACT:
return Object.assign({}, state, { actReceived: false, activities: null });
default:
return state;
}
}
我想知道它是怎么变成空的