[英]How to change a text inside an element, corresponding to an onClick event in ReactJs?
I am displaying two divisions corresponding, to two button's onClick event:我正在显示对应于两个按钮的 onClick 事件的两个分区:
class Home extends React.Component {
constructor() {
super();
this.state = {
isShowaMale: false
};
this.toggleShowMale = this.toggleShowMale.bind(this);
}
toggleShowMale(show) {
this.setState({ isShowaMale:show });
}
render() {
const { isShowaMale } = this.state;
return (
<div className="container py-5">
<div className="row">
<button className="btn col-6 bg-transparent col-12 col-sm-6" onClick={() => this.toggleShowMale(true)} >
<img className="humanbody profile" src={malebody} />
</button>
<button className="btn col-6 bg-transparent col-12 col-sm-6" onClick={() => this.toggleShowMale(false)} >
<img className="humanbody profile" src={femalebody} alt="" />
</button>
</div>
{/* Hidden div */}
<div className="row mx-auto">
{isShowaMale && (
<div className="mx-auto">
Hey man!
</div>
)}
{!isShowaMale && (
<div>
Hey woman!
</div>
)}
</div>
{/* Hidden div */}
</div>
)
}
}
export default Home;
But, can I just display one div and change just the word man
and woman
in the text Hey ____
?但是,我可以只显示一个 div 并只更改文本Hey ____
中的man
和woman
这个词吗? And there is also a problem that, after reloading the web page, it always shows Hey woman
due to isShowaMale: false
being default state.还有一个问题是,在重新加载 web 页面后,由于isShowaMale: false
默认为 state,它总是显示Hey woman
。 How can I solve these?我该如何解决这些?
[UPDATE] following further clarification in the comments, the issue can be handles in a way similar to this: [更新]在评论中进一步澄清后,可以以类似于以下方式处理该问题:
class Home extends React.Component {
constructor() {
super();
this.state = {
isShowaMale: false
};
this.toggleShowMale = this.toggleShowMale.bind(this);
}
toggleShowMale(show) {
this.setState({ isShowaMale:show });
}
render() {
const formProps = this.state.isShowMale
? { title: 'Mr', name: 'John', surname: 'Doe' }
: { title: 'Ms', name: 'Jane', surname: 'Doe' };
return (
<div className="container py-5">
<div className="row">
<button className="btn col-6 bg-transparent col-12 col-sm-6" onClick={()=> this.toggleShowMale(true)} >
<img className="humanbody profile" src={malebody} />
</button>
<button className="btn col-6 bg-transparent col-12 col-sm-6" onClick={()=> this.toggleShowMale(false)} >
<img className="humanbody profile" src={femalebody} alt="" />
</button>
</div>
{/* Hidden div */}
<div className="row mx-auto">
<div className="mx-auto">
<form>
<input type="text" id="title" name="title" placeholder={formProps.title} />
<input type="text" id="name" name="name" placeholder={formProps.title} />
<input type="text" id="surname" name="surname" placeholder={formProps.title} />
</form>
</div>
</div>
{/* Hidden div */}
</div>
)
}
}
export default Home;
Or the entire form can be placed into a separate function (or even a separate component).或者可以将整个表单放入单独的 function(甚至是单独的组件)中。
[ORIGINAL ANSWER] [原始答案]
You simply replace您只需更换
{/* Hidden div */}
<div className="row mx-auto">
{isShowaMale && (
<div className="mx-auto">
Hey man!
</div>
)}
{!isShowaMale && (
<div>
Hey woman!
</div>
)}
</div>
{/* Hidden div */}
with和
{/* Hidden div */}
<div className="row mx-auto">
<div className="mx-auto">
Hey {isShowaMale ? 'man' : 'woman'}!
</div>
</div>
{/* Hidden div */}
- Can I just display one div and change just the word man and woman in the text
Hey ____
我可以只显示一个 div 并只更改文本中的男人和女人这个词吗Hey ____
<div className="row">
{`Hey ${isShowMan? " man" : "woman"}!`}
</div>
- And there is also a problem that, after reloading the web page, it always shows Hey woman due to
isShowaMale: false
being the default state.还有一个问题是,在重新加载 web 页面后,由于isShowaMale: false
是默认的 state,它总是显示 Hey woman。
You can think about localStorage
你可以考虑localStorage
Due to Why are we disallowed to use HTML5 local storage on code snippets?由于为什么我们不允许在代码片段上使用 HTML5 本地存储?
So you can test the live demo所以你可以测试现场演示
constructor() {
super();
const isShow = window.localStorage.getItem("data");
this.state = { isShowMan: isShow === "false" || !isShow ? false : true };
}
toggleShowMan(isShow) {
window.localStorage.setItem("data", isShow);
this.setState({ isShowMan: isShow });
}
class Home extends React.Component { constructor() { super(); this.state = { isShowMan: true }; } toggleShowMan(isShow) { this.setState({ isShowMan: isShow }); } render() { const { isShowMan } = this.state; return ( <div className="container py-5"> <div className="row"> <button disabled={isShowMan} className="btn col-6 bg-transparent col-12 col-sm-6" onClick={() => this.toggleShowMan(true)} > malebody{" "} </button> <button disabled={.isShowMan} className="btn col-6 bg-transparent col-12 col-sm-6" onClick={() => this?toggleShowMan(false)} > femalebody </button> </div> <div className="row"> {`Hey ${isShowMan: " man"; "woman"}.`} </div> </div> ), } } ReactDOM.render(<Home />; document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.