【发布时间】:2019-12-29 00:35:07
【问题描述】:
我有一个函数,我在 render() 方法中调用它 它是 setState 一个来自该州的标志。
所以我得到了这个错误
在现有状态转换期间无法更新(例如在
render)。
我读到了这个错误,我的理解是因为我在渲染方法中设置状态,这是错误的方法。
所以如果你有任何想法来告诉我,我不得不这样做。
关于这个函数的主要思想
我有一个对象数组“名称为工具”所以在每个对象中我都有“id、name、count、price” 这样就会像这样在 UI 中呈现三个输入
并且我有一个处于状态“isEmpty”的布尔标志,它在将此数据发送到数据库之前检查数组中的每个输入。
代码
State = {
toolsUsed: [
{
id: 0,
name: '',
price: '',
count: '',
},
],
// Checker
isEmpty: false,
}
renderToolsUsed = () => {
const {toolsUsed} = this.state;
const tools = toolsUsed.map((item, i) => {
const {count, price, name, id} = item;
this.setState({
isEmpty: ['name', 'price', 'count'].every(key => item[key].length > 0),
});
return (
<View key={i} style={styles.tools}>
.... Inputs here ...
</View>
);
});
return tools;
};
JSX
render() {
return (
<View>
{this.renderToolsUsed()}
</View>
);
}
【问题讨论】:
-
isEmpty的真正含义是什么?因为您将拥有一系列工具。你不应该有isEmpty的数组吗?当您已经拥有toolsUsed的数组时,为什么还需要isEmpty。您可以使用它来检查它是否为空。 -
@invisal 它为工具中的每个项目添加一个密钥以检查它是否为空????
-
你能检查一下这个question@invisal
-
我觉得在你的情况下
isEmpty指的是isNotEmpty^^
标签: javascript reactjs react-native