Skip to content
Advertisement

Im trying to add an onClick function on the button so if the user clicks Yes, it removes the two buttons then add two new inputs field react js

this is the first code with the buttons elements

JavaScript

while this is where the button component is called and rendered, i think this is where the logic will happen

JavaScript

export default RightBody;

again what I want to do is that onClick of the yes button, I want to display two input fields instead of the two buttons originally there. Theres going to be a little icon by the side of the newly created input fields which when clicked takes the user back to the first case which is the two Yes and No buttons

JavaScript

This is my styles for the RightBody component this is how it should be befor the click

then after the click this

Advertisement

Answer

The key to solving this problem is to remember if the button was clicked as yet using React.useState().

Then, use the ternary operator <condition> ? <value if true> : <value if false> to conditionally control what elements need to be rendered.

JavaScript

Notice how I used a React Fragment <>...</> to wrap your two input boxes. These act like parentheses since you can only return one element at a time (with as many child nodes as you want)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement