Replies: 2 comments
-
In that case the simplest approach is to sync the observables to state, e.g. function useMyHook(store) {
const [val, setVal] = useState(() => store.observable)
useEffect(() => reaction(() => store.observable, v => setVal(v)), [])
// something with val
} As you can see that can easily be turned into it's own generic utility hook as well. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Another more natural way is to let the component observe and pass observed value(s) as param(s) to the custom
export const MySuperComponent: React.FunctionComponent = observer(() => {
const { myObservedValue } = myStore
const result = useMyHook(myObservedValue)
return <>Whatever the view is<>
}
export const useMyHook(myObservedValue) {
// ...
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Wrapping React components with
observer
seems to be required when interacting with observable values from MobX store.However, when I have most of my logic/utility written in a custom hook which requires some values from the store, the component that calls the hook must also be wrapped with
observer
. This is not very friendly, as there's no way to tell which of my custom hooks require the caller to be wrapped withobserver
.Is there a recommended guideline on this?
Beta Was this translation helpful? Give feedback.
All reactions