【发布时间】:2019-11-25 15:44:58
【问题描述】:
我的 FlatList 没有显示任何错误,但没有呈现我提供的数据。 redux 数组工作正常并向其中添加项目,但 FlatList 没有将它们呈现到屏幕上。
我是否给它 flex: 1 的样式和屏幕宽度,两者都没有区别。我不确定问题是否与我提供的数据有关,或者 renderItem 是否是问题。我有一种预感,redux 将数组传递给 FlatList 的方式存在问题,但不是肯定的。
我的主要 todo 组件的 FlatList 配置:
render() {
return (
<View style={{ height: HEIGHT }}>
<ScrollView>
<AddTodo
textChange={textInput => this.setState({ textInput })}
addNewTodo={this.addTodo.bind(this)}
textInput={this.state.textInput}
/>
<FlatList
style={{ flex: 1, width: WIDTH }}
data={this.state.todos}
extraData={this.props}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => {
console.log(item);
return (
<TodoItem
todoItem={item}
toggleDone={() => this.props.toggleTodo(item.id)}
removeTodo={() => this.props.removeTodo(item)}
/>
);
}}
/>
</ScrollView>
</View>
);
}
}
function mapStateToProps(state) {
return {
todos: state.todos
};
}
export default connect(mapStateToProps, actions)(MainTodo);
我的 todoItem 组件:
render() {
const todoItem = this.props.todoItem;
console.log(todoItem);
return (
<View>
<TouchableOpacity
style={styles.todoItem}
onPress={() => this.props.toggleDone()}
>
<Text
style={{
color: todoItem.completed ? '#aaaaaa' : '#313131',
textDecorationLine: todoItem.completed ? 'line-through' : 'none',
fontSize: 16 }}
>
{todoItem.text}
</Text>
<Button
title='Remove'
color='#ff5330'
onPress={this.props.removeTodo}
/>
</TouchableOpacity>
</View>
);
}
}
这就是我的 todo reducer 是如何为正在传递的数组设置的:
const todos = (state = initialState, action) => {
switch (action.type) {
case ADD_TODO:
return {
...state,
todos: [...state.todos, {
id: action.id,
text: action.text,
completed: false
}] };
添加待办事项工作正常,并将内容添加到我的数组中,但 FlatList 应该是空白的地方没有任何错误。
【问题讨论】:
标签: react-native redux react-redux