gpt4 book ai didi

javascript - 如何在 ReactJS 的嵌套组件中切换类

转载 作者:行者123 更新时间:2023-12-03 13:09:33 24 4
gpt4 key购买 nike

我的根组件中有这样的 render() 方法:

render: function() {
return (
<div className="question">
<QuestionA question={this.props.question} author={this.props.author}/>
<QuestionB yes={this.state.yes} no={this.state.no} />
<div className="question-side-switcher" onClick={this.handleSideChanging}></div>
</div>
);
}

当用户单击按钮时,我想在 QuestionA 和 QuestionB 组件之间切换“事件”类。我怎样才能做到这一点?请记住,QuestionA 和 QuestionB 在其 render() 方法中设置自己的类名。例如QuestionB的render():

render: function() {
return (
<section className="question-b-container">
...
</section>
);
}

最佳答案

有几种方法可以解决这个问题。

如果您希望父级控制附加类,您可以简单地将其传递给子组件,并让它们将其添加到现有的类名中 ( JSFiddle demo ):

var QuestionA = React.createClass({
render: function() {
return <section className={this.props.className + " question-a-container"}>Section A</section>;
}
});

var QuestionB = React.createClass({
render: function() {
return <section className={this.props.className + " question-b-container"}>Section B</section>;
}
});

var Root = React.createClass({
getInitialState: function() {
return { question: 'a' };
},

render: function() {
var qAclassName = this.state.question === 'a' ? 'active' : '';
var qBclassName = this.state.question === 'b' ? 'active' : '';
return (
<div className="question">
<QuestionA className={qAclassName} />
<QuestionB className={qBclassName} />
<div className="question-side-switcher" onClick={this.handleSideChanging}>Change</div>
</div>
);
},

handleSideChanging: function() {
this.setState({question: this.state.question === 'a' ? 'b' : 'a' });
}
});

但是,让子级管理类名并简单地发送一些数据来指示它是否应该设置其事件类( JSFiddle demo )可能更有意义:

// Using classSet to more easily create conditional class names;
// see http://facebook.github.io/react/docs/class-name-manipulation.html
var cx = React.addons.classSet;

var QuestionA = React.createClass({
render: function() {
// always set "question-a-container";
// set "active" if we got a truthy prop named `active`
var className = cx({
"question-a-container": true,
"active": this.props.active
});
return <section className={className}>Section A</section>;
}
});

var QuestionB = React.createClass({
render: function() {
// always set "question-b-container";
// set "active" if we got a truthy prop named `active`
var className = cx({
"question-b-container": true,
"active": this.props.active
});
return <section className={className}>Section B</section>;
}
});

var Root = React.createClass({
getInitialState: function() {
return { question: 'a' };
},

render: function() {
return (
<div className="question">
{/* For each question, compare to state to see if it's active. */}
<QuestionA active={this.state.question === 'a'} />
<QuestionB active={this.state.question === 'b'} />
<div className="question-side-switcher" onClick={this.handleSideChanging}>Change</div>
</div>
);
},

handleSideChanging: function() {
this.setState({question: this.state.question === 'a' ? 'b' : 'a' });
}
});

关于javascript - 如何在 ReactJS 的嵌套组件中切换类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com