score:1

Accepted answer

you should use usestate as said in the following way:

const [info, setinfo] = usestate({name: '' })

if (info.name === '') {
  setinfo({...info, name = 'empty'});
}

this will set info with only the change of the name property

score:0

a hook is something that starts with use like usestate. setstate is not a hook - it's a setter/updater. it can be used inside of a conditional statement. if it's done properly there shouldn't be any infinite loops.

score:0

in react functional components, usestate hook is used as an alternate to state in class components. the correct way to use the usestate hook is

const [ variable, setvariable ] = react.usestate( //default value );
    

the default value can be null, " string ", { object } , true / false, or 0, 1, 2 .......

and just like this.setstate() in class components, you set the state with the

setvariable( newvalue );

never ever try to change the state variables like you change the normal variables. they are immutable for one render and hence cause re-render when called setstate.

and for the infinite loop, please copy paste your component


Related Query

More Query from same tag