Reactjs 动态更改部分变量名?
在React中,是否可以根据状态动态更改部分变量名 例如,我正在从一个JSON文件中输入几个组件信息。根据所选择的性别(男性或女性),我想从“男装”或“女装”发送“atlas” JSX代码:Reactjs 动态更改部分变量名?,reactjs,Reactjs,在React中,是否可以根据状态动态更改部分变量名 例如,我正在从一个JSON文件中输入几个组件信息。根据所选择的性别(男性或女性),我想从“男装”或“女装”发送“atlas” JSX代码: class App extends Component { constructor(props) { super(props) this.state = { current_gender: "male", current
class App extends Component {
constructor(props) {
super(props)
this.state = {
current_gender: "male",
current_pants: 0,
}
<LimbSegment atlas={male_clothes[this.state.current_pants]['atlas']}/>
类应用程序扩展组件{
建造师(道具){
超级(道具)
此.state={
当前性别:“男性”,
当前_裤子:0,
}
我不确定我是否理解正确。
因此,如果state.gender
是男式
它将使用男式衣服
否则女式衣服
<LimbSegment atlas={this.state.gender === 'male' ?
male_clothes[this.state.current_pants]['atlas'] :
female_clothes[this.state.current_pants]['atlas']}/>
const{current_pants,gender}=this.state//我假设gender来自该州
康斯特服装=性别==‘男性’?男性服装:女性服装
const atlas=衣服[当前裤子]。atlas
//然后:
短的一个:
<LimbSegment atlas={(this.state.someFlag ? male_clothes : female_clothes)[this.state.current_pants]['atlas']}/>
但最好使用更详细的方法:
const clothes = this.state.someFlag ? male_clothes : female_clothes;
...
<LimbSegment atlas={clothes[this.state.current_pants]['atlas']}/>
const costs=this.state.someFlag?男装:女装;
...
那么您想根据什么状态更换男装?
const clothes = this.state.someFlag ? male_clothes : female_clothes;
...
<LimbSegment atlas={clothes[this.state.current_pants]['atlas']}/>