I have one reusable component which receives prop series. That prop contains the data object. Depends on data.length I need to update the component. In case one, data.lenght is 20, and in case two data.length is 6. But when I try to achieve this, component renders only the case where data.length is 20, and when I hit refresh it takes the second case where data.length is 6. I have tried using useEffect() hook but it doesn't work. Apparently I'm missing something. Here is what I've tried so far
const ReusableComponent = ({series}) => { const [propSeries, setSeries] = useState(series.data.length); useEffect(() => { if(propSeries > 6) { // do something } }, [propSeries]) } Here is the example of my problem: example. What am I doing wrong?
useStateonly gets initialized on the first render. SopropSerieswill never update unless you do it explicitly withsetSeries. Just like in class components: only use state if it is a value that will change over time, and is not a direct copy of props. Otherwise, just use the prop directly