Reputation: 6880
I have been struggling to show/hide components in ReactJs for a couple of days. I am trying to show the ("Write(blocks)" - which needs to be hidden from view by default) and shown when the "edit" links are clicked (while toggling hide / show for the "Read" blocks) and hide them when either the "save" or "cancel" buttons are clicked, I will be taking care of the save function later. For now I am just trying to show / hide the component based on this.
Below is my code:
class ProfileSettings extends React.Component {
render() {
return (
<div className="ProfileSettings">
<SettingsBlock className="Names" label="Name" link="name">
<p className="readOnlySettingField ReadNames">Hilal Agil<a href="#">Edit</a></p>
<div className="WriteNames">
<SubBlock label="First Name" placeholder="First Name" />
<SubBlock label="Middle Name" placeholder="Middle Name" />
<SubBlock label="Last Name" placeholder="Last Name" />
<p className="notice"><strong>Please note:</strong> You wont be able to change your name within the next 30 days.
Make sure not to add any unusual capitalization, punctuation, characters or random words.</p>
<button className="button small fill primary">Save Changes</button>
<button className="button small default">Cancel</button>
</div>
</SettingsBlock>
<SettingsBlock label="Username" link="username">
<p className="readOnlySettingField ReadUsername">www.squelo.com/hilarl<a href="#">Edit</a></p>
<div className="WriteUsername">
<p className="notice url">squelo.com/hilarl</p>
<Input placeholder="Username" classes="col-md-7" />
<p className="notice"><strong>Please note:</strong> Your username can only be changed once and should include your authentic name.</p>
<button className="button small fill primary">Save Changes</button>
<button className="button small default">Cancel</button>
</div>
</SettingsBlock>
<SettingsBlock label="Email" link="email">
<p className="readOnlySettingField ReadEmail">[email protected]<a href="#">Edit</a></p>
<div className="WriteEmail">
<Input placeholder="Email" classes="col-md-9" />
<p className="notice"><strong>Please note:</strong> Your username can only be changed once and should include your authentic name.</p>
<button className="button small fill primary">Save Changes</button>
<button className="button small default">Cancel</button>
</div>
</SettingsBlock>
<SettingsBlock className="Password" label="Password" link="password">
<p className="readOnlySettingField ReadPassword">Password last changed over a year ago<a href="#">Edit</a></p>
<div className="WritePassword">
<SubBlock label="Current" placeholder="Current" />
<SubBlock label="New" placeholder="New" />
<SubBlock label="Re-type new" placeholder="Re-type new" />
<p className="notice"><a href="#">Forgot password?</a></p>
<button className="button small fill primary">Save Changes</button>
<button className="button small default">Cancel</button>
</div>
</SettingsBlock>
</div>
);
}
}
Would be great if somebody could give an example for how to achieve this in this situation. I have been wasting a lot of time figuring this out myself and this is the first time I am using ReactJs in a project. Thanks.
Upvotes: 0
Views: 960
Reputation: 4945
This is the pattern I would use.
render() {
var hideWriteBlock = (show hide logic);
var hideReadBlock = (show hide logic);
return (
<div className="ProfileSettings">
<SettingsBlock className="Names" label="Name" link="name" hide={hideWriteBlock}>
In the settings component;
render() {
if (this.props.hide) return null;
return (
<div>
{this.props.children}
</div>
)
}
I use hide so that I don't have to write if (!this.props.show) return null;.
Upvotes: 2