Codementor Events

Switching Between Different Button Modes In React Js

Published Aug 27, 2019
Switching Between Different Button Modes In React Js

To develop this visibility toggle all we need is just a single button which would toggle two different texts based on the function of the button, now it would appear in the UI that there are two different buttons, but there is just a single button which would have a boolean that states or print on the screen if the following statement is true or false, so we code it like this :

Note that we should have already created a root id in the HTML to link it up with the react app

Const exampleTemplate = (
<div>
<h1> Example Header</h1>

Above we have the header, & below we have the single button needed, we create an onClick function:

<button onClick={exampleClickFunction}>
     {exampleClickVisible ? ‘example hide text’ : ‘example view text’}
</button>

Now in the above button the expression used is a spread operator

</div>
{exampleVisible && (
<div>
<p>here is the text</p>
</div>)}
);

//so we create a variable containing the boolean and an on click function that triggers the button //change in accordance to the boolean and the visibility toggle.

const exampleVisible = false;
const exampleClickFunction = () => {
exampleVisible= !exampleVisible
}

This function simply changes the value of the variable to not.
Now, all we need to do for this to function is to rerender the code into a function.

const exampleRender = () => {
const exampleTemplate = (
<div>
<h1> Example Header</h1>

Above we have the header, & below we have the single button needed, we create an onClick fn.

<button onClick={exampleClickFunction}>
   {exampleClickVisible ? ‘example hide text’ : ‘example view text’}
</button>
</div>

Now in the above button, the expression used is a spread operator.

{
  exampleVisible && (
   <div>
    <p>here is the text</p>
  </div>
 )}
);

We create a variable containing the boolean and an on click function that triggers the button, change in accordance to the boolean and the visibility toggle.

let exampleVisible = false;
const exampleClickFunction = () => {
    exampleVisible= !exampleVisible
}

This function changes the value of the variable to the opposite boolean.

ReactDOM.render(exampleTemplate, document.getElementById(appRoot))
};
Discover and read more posts from Nwoga kingsley
get started
post commentsBe the first to share your opinion
Sonyaansen
4 years ago

Switching between different button modes in react is little bit easy. But it is easy for the time when you start to understand it as a perfect essayshark.com review theme which is define here. Hope you will take all these modes which are heaving more react for us.

Arsen Gutsal
5 years ago

@Nwoga Kingsley Guess there are some typos: Const exampleTemplate should read const, {exampleClickVisible ? ‘example hide text’ : ‘example view text’} - plz, change reversed single quotes to single/double ones, as someone doing copy/paste would get a problem.

Nwoga kingsley
5 years ago

thank you, would correct that next time

Show more replies