-
I wasn't able to find examples that use mobx in React functions so started researching and playing and eventually settled on this. It's based on the Egghead videos Michel posted. import * as React from 'react';
import { observable } from 'mobx';
import { observer } from 'mobx-react-lite';
interface StoreType {
count: number;
increment: () => void;
decrement: () => void;
}
const countStore = observable<StoreType>({
count: 0,
increment: function() {
this.count ++;
},
decrement: function() {
this.count --;
},
});
function CounterFunctionalComp(
countStore: {
countStore: {
count: number;
increment: () => void;
decrement: () => void;
}
}
){
const onClick=(e:React.SyntheticEvent)=>{
if (e.currentTarget.innerHTML === '+'){
countStore.countStore.increment();
}
else{
countStore.countStore.decrement();
}
}
return (
<div>
<div>{countStore.countStore.count}</div>
<button onClick={onClick}>+</button>
<button onClick={onClick}>-</button>
</div>
);
};
const FunctionalCompObserver = observer(CounterFunctionalComp);
const ReactFunkyComponent = () => <FunctionalCompObserver countStore={countStore} />
export default ReactFunkyComponent; ☝️
|
Beta Was this translation helpful? Give feedback.
Answered by
kubk
Apr 30, 2022
Replies: 1 comment 3 replies
-
Have you checked Mobx docs? https://mobx.js.org/README.html#a-quick-example |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
kubk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you checked Mobx docs? https://mobx.js.org/README.html#a-quick-example