React native 如何在全局变量React Native中设置数组值
我正在创建react本机应用程序。我想将我的数组值设置到声明为此的变量中。状态:{current_Cat_id:'',}。我的代码如下:-React native 如何在全局变量React Native中设置数组值,react-native,React Native,我正在创建react本机应用程序。我想将我的数组值设置到声明为此的变量中。状态:{current_Cat_id:'',}。我的代码如下:- render() { return( {this.props.data.map((dataImage,Index)=> <View key={Index}> {dataImage['main-head'] != undefined &&
render() {
return(
{this.props.data.map((dataImage,Index)=>
<View key={Index}>
{dataImage['main-head'] != undefined && (
<View style={{marginRight:20,marginLeft:20,marginBottom:20,width:320,fontSize:16,color:'#000'}}>
{dataImage['main-head'].map((subsubchild, Index3)=>
<Text>{subsubchild['cat_name']} ({subsubchild['num_rows']})
</Text>
)}
</View>
)}
</View>
)}
)
}
render(){
返回(
{this.props.data.map((dataImage,Index)=>
{dataImage['main-head']!=未定义&&(
{dataImage['main-head'].map((子子子项,Index3)=>
{subsubchild['cat_name']}({subsubchild['num_rows']})
)}
)}
)}
)
}
当我从循环中获取值时,它应该是变量中的设置值,即在this.state中声明的值。
我试过这样做:
{this.props.data.map((dataImage,Index)=>
<View key={Index}>
{dataImage['main-head'] != undefined && (
<View style={{marginRight:20,marginLeft:20,marginBottom:20,width:320,fontSize:16,color:'#000'}}>
{dataImage['main-head'].map((subsubchild, Index3)=>
this.setState:({current_cat_id:subsubchild['cat_name']})
<Text>{subsubchild['cat_name']} ({subsubchild['num_rows']})
</Text>
)}
</View>
)}
</View>
)}
class ReactClass extends Component {
constructor(props) {
super(props)
this.state = {
# put initial values of state here or make them depend on props
}
}
componentWillMount() {
# access this.props here and use this.setState as needed
}
}
{this.props.data.map((dataImage,Index)=>
{dataImage['main-head']!=未定义&&(
{dataImage['main-head'].map((子子子项,Index3)=>
this.setState:({current_cat_id:subsubsubchild['cat_name']})
{subsubchild['cat_name']}({subsubchild['num_rows']})
)}
)}
)}
但是我不能成功。我怎样才能做到这一点基兰
最佳做法是不修改render()中的状态。render()应该使用状态和道具来确定呈现的HTML是什么
如果您希望基于props设置状态,那么最好在构造函数或componentWillMount()生命周期方法中进行设置。您可以这样做:
{this.props.data.map((dataImage,Index)=>
<View key={Index}>
{dataImage['main-head'] != undefined && (
<View style={{marginRight:20,marginLeft:20,marginBottom:20,width:320,fontSize:16,color:'#000'}}>
{dataImage['main-head'].map((subsubchild, Index3)=>
this.setState:({current_cat_id:subsubchild['cat_name']})
<Text>{subsubchild['cat_name']} ({subsubchild['num_rows']})
</Text>
)}
</View>
)}
</View>
)}
class ReactClass extends Component {
constructor(props) {
super(props)
this.state = {
# put initial values of state here or make them depend on props
}
}
componentWillMount() {
# access this.props here and use this.setState as needed
}
}
编辑
尝试此操作以使状态具有基于映射的数组如何:
componentWillMount() {
var ids = [];
this.props.data.map((dataImage) => {
ids.push(dataImage['ProductName'])
})
this.setState({current_cat_id: ids})
}
这可能并不完全有效,但这是我建议您构建列表然后立即设置状态的一般方法 嗨,Kiran,你想通过把它放到你的州来实现什么?在render()函数中设置您的状态实际上是违反react主体的。render()函数应该使用状态来呈现html,而不是修改状态。如果您解释了为什么要尝试设置状态,也许我们可以想出一种在render()之外进行设置的方法,这更有意义。如何在render之外使用此.props.data.map?这取决于您尝试执行的操作。例如,是否要在每次更改道具时更新状态?因为这样您就可以将代码放入生命周期函数组件WillReceiveProps中。如果您只想在开始时设置它,可以将它放在ComponentWillmount中。我在ComponentWillmount中尝试过这样的方法:this.props.data.map((dataImage)=>alert(dataImage['main-head']),但是它的返回错误应该可以,并且允许您正确设置状态。您仍然遇到问题吗?我正在这样尝试:componentWillMount(){this.props.data.map((dataImage)=>this.setState({current\u cat\u id:dataImage['ProductName']});)}及其返回的意外标记(预期),为什么在循环数据时设置状态?这将导致在每次迭代中覆盖状态。运行映射后,您希望this.state.current_cat_id的值是多少?在数组中,我有类别id,我想在变量当前_cat_id中设置它。我想在映射之外使用它。您希望当前_cat_id是所有类别id的列表吗?我想使用循环之外的值