我有状态,我想在数组计数中使用这个,但我做不到。我使用反应导航。也许你知道另一个例子。
constructor(props){
super(props);
this.state = {
whileI: this.props.navigation.getParam('i'),
count: this.props.navigation.getParam('count'),
countTrue: 0,
}
}
static navigationOptions = {
header: (
<View style={{height: "10%", backgroundColor: '#1194F6', elevation: 5, justifyContent: 'center', flexDirection: 'row'}}>
{Array.from(Array(this.state.count).keys()).map(idx => (
<View key={idx} style={{ flex: 1,
borderWidth: 1,
borderColor: '#767',
marginLeft: 2,
marginTop: "8%",
marginRight: 2,
marginBottom: 5,
backgroundColor: '#C5C5C5',
backgroundColor: '#F8F',
}}></View>
))}
</View>
)
}
在这种情况下,您可以尝试从导航参数中获取值,以便可以将导航选项作为函数重写。下面是如何执行此操作的示例:https://reactnavigation.org/docs/en/header-buttons.html
此外,另一种方法 - 使用一些 HOC 将数据作为 props 传递给屏幕组件(或使用上下文 api 或 redux)。这取决于您的需求。