javascript - Flatlist 不显示嵌套数组
问题描述
所以我试图在 Flatlist 中显示一个数组,我正在使用 Redux 更新初始状态,我通过在 LIST CREATION 屏幕中收集所有数据来做到这一点.push
,然后我将它们插入到数组中,然后我使用updateList
更新的初始状态list
,我用来getDerivedStateFromProps
在 TRIALLIST 屏幕中获取该列表并将其显示在 aFlatlist
中,问题是我无意中创建了嵌套数组,并且它不允许我在 Flatlist 中显示数据,这是示例我要显示的数组:
Array [
Array [
Object {
"Name": Object {
"Name": "John",
},
"key": 0.05992611071666909,
"favNumber": 1,
"age": 30,
},
],
]
这里有各种屏幕:
列表创建
import { connect } from 'react-redux';
import { updateList } from '../../../../../redux/actions/index.js';
class trial extends Component {
constructor(props) {
super(props);
this.state = {
trial: '',
list: [],
};
}
submitTrial(){
let list = this.state.list;
list.push({
key: Math.random(),
Name: this.props.route.params,
favNum: favNum,
age: age,
});
this.props.updateList(list);
this.props.navigation.navigate("TrialList");
}
render() {
return (
<Button transparent>
<Icon
name="checkmark"
onPress={() => this.submitTrial()}
/>
</Button>
const mapDispatchToProps = { updateList };
export default connect( mapDispatchToProps )( trial );
试用者
class TrialList extends Component {
constructor(props) {
super(props);
this.state = {
list: [],
};
}
static getDerivedStateFromProps(props, state) {
if (props?.list) {
return {
list: [...state.list, props.list],
};
}
return null;
}
render() {
return (
<FlatList
data={this.state.list}
keyExtractor={(item, index) => item.key.toString()}
contentContainerStyle={{ flexGrow: 1 , flexGrow: hp('20%')}}
renderItem={({ item }) => (
<View>
<View>
<Text>
{item.Name.Name}
</Text>
<Text>
{item.favNumber}
</Text>
<Text>
{item.age}
</Text>
</View>
</View>
/>
function mapStateToProps(store){
return{
list: store.userState.list
};
}
export default connect(mapStateToProps)(TrialList);
索引.JS
import { ADD_LIST } from "../constants/index";
export const updateList = (list) => {
return console.log({ type: ADD_LIST, payload: list})
}
减速器
import { USER_STATE_CHANGE, ADD_LIST } from "../constants";
const initialState = {
currentUser: null,
list: null,
};
export const user = (state = initialState, action) => {
switch (action.type){
case USER_STATE_CHANGE:
return {
...state,
currentUser: action.currentUser,
};
case ADD_LIST:
return{
...state,
list: [...action.payload],
}
default:
return state
}
};
在此先感谢您的帮助。
解决方案
推荐阅读
- flutter - 获取初始路由颤动的查询参数
- sml - SML:为什么返回类型是单位?
- vue.js - 获取的数据在代理中返回,而不是在“正常”对象中
- c++ - std::async 的非阻塞调用:这个版本有多危险?
- node.js - nodejs异步模块导出
- c++ - 在嵌套循环中避免过多的“声明 Rcpp 列表的相同元素”
- python - 辣椒机器人语音识别
- r - 是否有 R 函数允许我将伴随变量添加到具有混合分布的潜在聚类分析中?
- java - 服务器端的 Apache cxf Digest 身份验证
- flutter - 如何像 Flutter 中的专业人士一样从一个小部件平滑过渡到另一个小部件?