score:3

Accepted answer

are there any side effects to this? why would this work and not my original attempt?

no there are no side effects, that is the way to go in your situation. it didn't work in your case because you violated one of the rules of hooks:

don’t call hooks inside loops, conditions, or nested functions. instead, always use hooks at the top level of your react function, before any early returns. by following this rule, you ensure that hooks are called in the same order each time a component renders. that’s what allows react to correctly preserve the state of hooks between multiple usestate and useeffect calls. (if you’re curious, we’ll explain this in depth below.)

initially your usestate calls themselves were wrapped inside a condition and that was a problem. that is not the case anymore with your newer approach.

score:0

if data can ever change.... the only thing you need to make sure you handle is that the data being passed down is not null, but rather undefined if it doesn't exist.

your useeffect should have data in the dependency array to re-render the component upon any change.

const calculate({data = 0}) => {
  const [number, setnumber] = usestate(data);

  useeffect(() => {
     setnumber(data)
   }, [data]);
} 

score:8

you can use a ternary inside your usestate hook and it will work fine. however, it is better to wrap it inside a function expression in the following manner.

const [number, setnumber] = usestate((data) => data === 'end' ? 5 : 0)

the function expression in above code snippet will be evaluated only once while setting the initial state. there is an upside to wrapping your ternary operator inside the function expression.

suppose that instead of data === 'end' ? 5 : 0 you want to use the results of an expensive computation to set the initial state, in such a case you can wrap it inside a function expression like:

const [number, setnumber] = usestate(() => expensivefunction())

this is better because () => expensivefunction() will return a function which will only be evaluated once during the initial render. whereas if you don't wrap the expensivefunction inside a function expression, it will be evaluated each time your component renders and block the execution of code until expensivefunction returns a value.

this approach is also known as lazy initialization.


Related Query

More Query from same tag