"react-native": "0.70.2",
"@legendapp/state": "^0.19.0",
"react": "18.1.0",
"typescript": "^4.8.2"
I'm noticing observers being called an increasing number of times after fast refresh.
After a fresh reload I get the logs:
ComponentA hook observer undefined
ComponentA hook observer 1665157345629
if I save ComponentA.tsx without making any changes fast refresh triggers and I get an addditional single console.log:
ComponentA hook observer 1665157345629
If I then go and save ComponentB.tsx without making any changes fast refresh triggers and I get 2 console.logs with newer timestamps
ComponentA hook observer 1665157432979
ComponentA hook observer 1665157432979
If I then go back to save ComponentA.tsx again and save without making any changes I get an addditional single console.log:
ComponentA hook observer 1665157432979
Then if I go back and save ComponentB.tsx without making any changes, I get 3 console.logs with newer timestamps
ComponentA hook observer 1665157525844
ComponentA hook observer 1665157525844
ComponentA hook observer 1665157525844
This will continue, every time I save ComponentA it seems to add another observer, which then gets triggered if I save somewhere else. Interestingly if I save ComponentA 10 times in a row before saving ComponentB, it only seems to add 1 more observer log.
Any idea what's happening?
App.tsx
import { enableLegendStateReact } from '@legendapp/state/react';
import React from 'react';
import ComponentA from './src/test/ComponentA';
import ComponentB from './src/test/ComponentB';
// Enable direct rendering of observables
enableLegendStateReact();
const App = () => {
return (
<>
<ComponentA />
<ComponentB />
</>
);
};
export default App;
ComponentA.tsx
import state from '../state';
const ComponentA = () => {
useObserve(() => {
console.log('ComponentA hook observer', state.settings.test.get());
});
return null;
};
export default ComponentA;
ComponentB.tsx
import {useEffect} from 'react';
import state from '../state';
const ComponentB = () => {
useEffect(() => {
state.settings.test.set(Date.now().toString());
}, []);
return null;
};
export default ComponentB;
state.ts
import settings from '../state/settings';
export default {settings, sites};
settings.ts
const settings = observable({
test: undefined as string,
});
export default settings;