/    Sign up×
Community /Pin to ProfileBookmark

react.JS – hide/show component

Hello

I am new to reactJS.

I wonder if it is possible to hide/show components “onclick”? For example: index.php shows some text in headings and paragraphs(a component), a button or link can be found under the text. if the link or button is clicked, the text will change(new headings and paragraphes, aka a new component). How can this be done?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottJan 25.2018 — Short answer, in your onclick handler, call setState. The headings and paragraphs you show could come from the state, so when you change the state, then your DOM automatically re-renders.

Here's an example from the [url=https://reactjs.org/tutorial/tutorial.html][U]React tutorial[/U][/url]:

<i>
</i>class Square extends React.Component {
constructor(props) {
super(props);
this.state = {
value: null,
};
}

render() {
return (
&lt;button className="square" onClick={() =&gt; this.setState({value: 'X'})}&gt;
{this.state.value}
&lt;/button&gt;
);
}
}
Copy linkTweet thisAlerts:
@stianokern1337Mar 08.2019 — A basic way to toggle onClick to show or hide.

Just replace the 'SHOW' and 'HIDE' width the divs you want to show or hide.

Hope it helps! And contact if you need some more help ?

import React, { Component } from 'react'

export default class Test extends Component {

constructor(props) {

super(props);

this.state = {

value: false

}

this.onClickHandler = this.onClickHandler.bind(this);

}

onClickHandler = () => {
this.setState (state => ({
value: !state.value
}))
}

render() {

return (

<div>

<button onClick={this.onClickHandler}>

{this.state.value ? 'SHOW' : 'HIDE'}

</button>

</div>

)

}

}
Copy linkTweet thisAlerts:
@rootMar 08.2019 — {"locked":true}
Copy linkTweet thisAlerts:
@rootMar 08.2019 — PLEASE DO NOT RESURRECT OLD POSTS,

*locked*
×

Success!

Help @jag1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...