Javascript 将活动类添加到所选项目

Javascript 将活动类添加到所选项目,javascript,reactjs,Javascript,Reactjs,我制作了一个react父组件,如果用户单击这两个选项中的一个,就会呈现一个不同的子组件 根据选择的选项,我想添加一个活动的类 请参阅下面的代码 我已经采取了很多方法,但由于我无法理解这个问题,我已经把它们去掉了 class Test extends Component { constructor(props) { super(props); this.state = { currentView: 'one' };

我制作了一个react父组件,如果用户单击这两个选项中的一个,就会呈现一个不同的子组件

根据选择的选项,我想添加一个
活动的

请参阅下面的代码

我已经采取了很多方法,但由于我无法理解这个问题,我已经把它们去掉了

class Test extends Component {

    constructor(props) {
        super(props);

        this.state = {
            currentView: 'one'
        };

        this.showOne = this.showOne.bind(this);
        this.showTwo = this.showTwo.bind(this);
    }

    showOne() {
        this.setState({
            currentView: 'one'
        });
    }

    showTwo() {
        this.setState({
            currentView: 'two'
        });
    }

    render(){
    ......
        <p className="one" onClick={this.showOne}>One</p>
        <p className="two" onClick={this.showTwo}>Two</p>
    ......
    }
}
类测试扩展组件{
建造师(道具){
超级(道具);
此.state={
当前视图:“一个”
};
this.showOne=this.showOne.bind(this);
this.showtow2=this.showtow2.bind(this);
}
showOne(){
这是我的国家({
当前视图:“一个”
});
}
showTwo(){
这是我的国家({
当前视图:“两个”
});
}
render(){
......

one

two

...... } }
你在找这样的东西吗

<p className={this.state.currentView === 'one' ? 'one active' : 'one'} onClick={this.showOne}>One</p>
<p className={this.state.currentView === 'two' ? 'two active' : 'two'} onClick={this.showTwo}>Two</p>

one

two


就是这个。穆查斯·格雷西亚斯·霍姆布雷!:-)如果您可以使用ES6模板文本:
className={'one${this.state.currentView=='one'?'active':''}}
,避免重复
one
class