Home > Net >  I have 2 texts list and grid.Initially it is list,it will show grid when click but i want to show li
I have 2 texts list and grid.Initially it is list,it will show grid when click but i want to show li

Time:11-21

Initially I setted the text as list in use-state and it change to grid when I click on it.How to show the list again when I click?

const [buttonText, setButtonText] = useState('List');
function handleClick() {
    setButtonText('Grid');

};

This is the use-state and the function that changes the text.Can anyone help me ?

CodePudding user response:

You can call same method and use ternary operator (check your current state and setState according to it).

function handleClick() {
  setButtonText(buttonText == 'Grid' ? 'List' : 'Grid');
};
  • Related