0

Is the state variable redundant in the following React Native code?

export default function Test(props) {

    const [number, setNumber] = useState(props.number);

    return(
        <Text>{number}</Text>
    )
}

What is considered best practice, and why?

jpryor
  • 31
  • 1
  • 3
  • Absolutely redundant if you never call the state setter - and if you do call the state setter, you'll have to keep in mind that it doesn't change the parent component state that's passing down the prop. There are surely rare cases for this sort of thing, but it's usually not a good idea I think. – CertainPerformance Jun 23 '22 at 04:30
  • The only benefit from this is if you want to use the initial value of the prop as a default value, and don't want it to change when the props change. – Charchit Kapoor Jun 23 '22 at 04:48

0 Answers0