作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的根组件中有这样的 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/
我是一名优秀的程序员,十分优秀!