You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, if we have 2 components - Component1, Component2;
Component1.ts
const[username,setUsername]=useState('')/* all rest logic here */<Form><Inputid="username"onChange={handleOnChange}value={username}/><ButtononClick="handleSubmit"></Form>
Now - we are closing Component1 with last username - let's say XYZ. Normally - it should be removed by some GC.
We are going further, and opening Component2, which should be completely independent from Component1.
Component2.ts
exportdefaultfunctionSecondComponent(){const[myState,setMyState]=useState('TEST STRING');console.log(myState);// it is XYZ, from Component1 state.
As you can see - a tricky thing happens, as I am getting "XYZ" despite it should be TEST STRING. Also it works in second direction. If I will change state in Component2 and render Component1 again; input #username takes the value from Component2 state.
This is the correct behavior from the framework's perspective -- you should never invoke components directly (page()), they expect (and need to be) called from the context of the framework.
constApp=()=><div>foo</div>;// DON'T: Invoking components directly breaks hooks and update ordering:render(App(),rootElement);// ERRORrender(App,rootElement);// ERROR// DO: Passing components using h() or JSX allows Preact to render correctly:render(h(App),rootElement);// successrender(<App/>,rootElement);// success
Describe the bug
Imagine we have an array with components we should render:
We render them like that:
Now, if we have 2 components - Component1, Component2;
Component1.ts
Now - we are closing Component1 with last username - let's say XYZ. Normally - it should be removed by some GC.
We are going further, and opening Component2, which should be completely independent from Component1.
Component2.ts
As you can see - a tricky thing happens, as I am getting "XYZ" despite it should be TEST STRING. Also it works in second direction. If I will change state in Component2 and render Component1 again; input #username takes the value from Component2 state.
If I render these pages like that:
Nothing wrong happens; state is isolated properly for component scope.
To Reproduce
Described above
The text was updated successfully, but these errors were encountered: