我想根据状态值显示组件。我的
import React, {Component} from 'react';
import One from './One'
import Two from './Two'
import Three from './Three'
class MyTest extends Component{
constructor(props){
super(props);
this.state = {
slide: 1
}
}
handleClick=()=>{
const counter=this.state.slide;
this.setState({
slide:counter+1
})
}
render(){
return (
<div onClick={this.handleClick}>
{this.state.slide===1 ? <One /> :
{this.state.slide===2 ? <Two /> : <Three /> } }
</div>
)
}
}
export default MyTest;
我收到错误:for {this.state.slide===2
解析错误:意外的关键字“this”
我做错了什么?
最佳答案
内部三元内部不需要{和}
{this.state.slide===1 ? <One /> :
this.state.slide===2 ? <Two /> : <Three /> }
关于javascript - 三元运算符的附加条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57836403/