Replies: 4 comments 4 replies
-
Regarding the first idea. A common pattern is to fetch data on component mount like this: const Component = observer(() => {
const { userStore } = useStore();
useEffect(() => {
userStore.fetch();
}, [userStore]); // pass argument to avoid react eslint warning
}) Since
In my opinion passing store to useEffect arguments is OK. I agree that passing an observable value to useEffect is wrong. For example |
Beta Was this translation helpful? Give feedback.
-
Btw both ideas play nice with const store = makeAutoObservable({ action() {} });
configure({ observableRequiresReaction: true })
useEffect(() => {
store.action();
}, [store]) because On the other hand, this wouldn't work correctly for idea 2 useObserverEffect(/*here*/action(() => {
store.action();
}), [store]) because |
Beta Was this translation helpful? Give feedback.
-
We definitely shouldn't hit the performance with idea 2. |
Beta Was this translation helpful? Give feedback.
-
Another possible problem with idea 2 are async calls: useObserverEffect(() => {
fetch('')
.then(() => {
// Untracked, but the result potentially depends on this
console.log(obj.x);
})
}, [obj]) I don't really like this and I don't see a way around that. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
useEffect(() => mobxStore.a, [mobxStore])
is an error, becausemobxStore
isn't immutable, buteslint-plugin-react-hooks
doesn't know and therefore doesn't warn.useMemo(() => mobxStore.a, [mobxStore])
callback runs within observer, therefore component subcribes formobxStore.a
, but a change ofmobxStore.a
only causes re-render without updating the memoized value.Idea 1
Expose our own version of these hooks that check you do not pass
observable
as depenency:Idea 2
Expose our own version of these hooks that are reactive:
This is more convinient, but has a runtime cost.
Keep in mind the idea is not to avoid the need to pass dependencies or to provide some optimization benefits. The idea is just to make sure mobx plays well with
eslint-plugin-react-hooks
and the way the hooks are designed.In both cases we can introduce
eslint
rule, that forbidsReact.useFoo
andimport { useFoo } from React
.EDIT: I think we can limit this to uses inside
observer
(since we can assume there are no observables without observer)Beta Was this translation helpful? Give feedback.
All reactions