Example is simple functional component that use useState to keep click counter.
Stepping through Stepper MUI component, I want to create instance of Example component with different init value e.g. at step 0, init value 100, at step 1, init value 111, at step 2, init value 112.
While stepping through each case of step, despite passing different initial value, Example functional component keep state only to first init value i.e. 100.
File is /Components/Navigation/Stepper01a.js, Example component being referenced in StepContent, which is being referenced in HorizontalLinearStepper component. Overall code is example from Material UI Stepper component. Am just trying to test it to create different instance of other functional component (Example in this case) with different initial values at each step.
Example component:
function Example({ init }) {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = React.useState(init)
return (
<div>
<p>init {init} </p>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
)
}
StepContent Component:
function StepContent({ step }) {
console.log("step", step)
switch (step) {
case 0:
return <Example init={100} />
case 1:
return <Example init={111} />
case 2:
return <Example init={112} />
default:
return "Unknown step"
}
}
HorizontalLinearStepper Component:
export default function HorizontalLinearStepper() {
...
<div>
<StepContent step={activeStep} />
</div>
...
}
See running example https://mj3x4pj49j.codesandbox.io/ code https://codesandbox.io/s/mj3x4pj49j
Upon clicking Next at step 0, count is expected to set with init value as 111 but it remains 100 and upon clicking Next at step 1, count is expected to set with init value 112 but again it remains 100. It seems that once state count is initialized to 100 at step 0, same state is being used in step 1 and 2 i.e. state is not isolated.
Does this problem occur due to violation of Rules of Hooks somehow?