0

I'm just starting with react and hooks and I terribly fail writing conditionals in the dom related return. How can I do this? The chevron should change depending on the toggle.

const Description = () => {
    const {t} = useTranslation();
    const [isToggled, setToggled] = useState(false);
    const toggleTrueFalse = () => setToggled(!isToggled);
    return (
        <div>
            {if isToggled == true }{
            <ChevronRight className="arrow" size="20"/>
            } else{
            <ChevronDown className="arrow" size="20"/>
            }
        </div>

       );
    };
export default Description;
Dupocas
  • 20,285
  • 6
  • 38
  • 56
Jurudocs
  • 8,595
  • 19
  • 64
  • 88

1 Answers1

2
return (
        <div>
            {isToggled ? <ChevronRight className="arrow" size="20"/> :  <ChevronDown 
             className="arrow" size="20"/>}

        </div>

       );

In return you cannot use if conditions. if-else statements don't work inside JSX. This is because JSX is just syntactic sugar for function calls and object construction. You got to use {} inorder to make a conditional decision. Hope this helps!

Ram Sankhavaram
  • 1,218
  • 6
  • 11