I had achieved it, but I deleted the code by mistake... with the following example it would help me to show 2 states.
import React, { useState } from 'react'; export default () => { const [show, setShow] = useState(true); return ( <> <button type="button" onClick={() => { setShow(!show); }} > Mostrar {show ? 'Div 2' : 'Div 1'} </button> {show ? ( <div style={{ color: 'red' }}>Div 1</div> ) : ( <div style={{ color: 'blue' }}>Div 2</div> )} </> ); };
But I have a dropdown list with four options, and depending on which one I select, it will show different fields on the form.
import "./styles.css"; import React, {useState} from "react"; export default function App() { const [animal, setAnimal] = useState(null); return ( <div className="App"> <> <div> <label htmlFor="tipoId" className="col-md-6"> Select Animal </label> <select onClick={() => setAnimal(!animal)}> <option value="0" selected hidden>Horse</option> <option value="1" selected>Tiger</option> <option value="2">Lion</option> <option value="3">Panther</option> </select> </div> { animal === "1" ? <> <div> TIGER</div> </> : animal === "2" ? ( <> <div>LION</div> </> ): ( <> <div>PANTHER</div> </> ) } </> </div>); }
As you can see, the current code does not work, since it will only allow to have 2 states, show by default the panther ones and if another option is selected it will only show the tiger one, but the lion fields are never shown. Previously I had even been able to include in the drop-down list to be able to choose 6 options and that when choosing these they would show fields for this respective option.
I need to get to that point again :(