-
-
Notifications
You must be signed in to change notification settings - Fork 46
link to codesandbox demo of useContext example #7
base: master
Are you sure you want to change the base?
Conversation
To understand the example code in more detail, I implemented it in codesandbox. Here is a github repo if you want to fork it: https://github.com/100ideas/mobx-react-usecontext-codesandbox-demo I was unable to get the original `createStore()` function to work as an observable, so I changed it slightly to an `observable(pojo)`. You should review it before accepting this PR to make sure what I changed is how you intend this pattern to be implemented. ``` createStore() { // storing variable in closure means you can access it in methods // otherwise you would have to use `this` const store = { //... ```
codesandbox link will always use fresh code from my master branch github repo https://codesandbox.io/s/github/100ideas/mobx-react-usecontext-codesandbox-demo
Funny, I was thinking about doing exactly this today :) You are right about export const StoreProvider = ({ children }) => {
const store = useLocalStore(createStore) The way you did it isn't wrong, but I dislike the use of That said I would prefer if example in CodeSandbox would be written with TypeScript since the original is like that as well. If you are not feeling up to that, I will make it happen sometimes later. Good stuff either way. |
I get it about The discussion about the changing API in mobx-react & mobx-react-lite has been moving so fast that I wasn't sure if I don't really like typescript for prototyping b/c I find it generally slows me down with a bunch of syntax and composite type errors - but this is because I just don't use/understand typescript much. Here is an updated codesandbox with your suggested changes, I am not sure why it isn't working: https://codesandbox.io/s/mobxreact-usecontext-provider-demo-ls076 |
Later today I am going to try extending this example into an app that demonstrates how to create multiple stores that manage models that relate to one another. The "model(pojo)-view(react)-controller(mobx)" pattern. examples of pattern:
relevant discussion: #3 (comment) Stores are singletons. Root store has an // PostStore is mobx observable object or class instance w/ ref to rootStore
PostStore.deletePost = function( postId ){
const { commentIds } = PostStore.posts.get( postId )
PostStore.RootStore.CommentStore.removeByIds( commentIds )
PostStore.posts.remove( postId );
} If you have any suggestions about how NOT to implement this, or preferred patterns that I could use, please let me know! |
Thank you for the effort of converting to TypeScript. I kinda consider myself TypeScript messenger and want to try to spread the word as much as I can so people can see it's nothing to be afraid of. On the contrary, it has already helped me in so many tricky situations that it's definitely worth it. The example is not working simply because of all files with JSX has to be TSX :) I used to fight with that as well, but it's not that hard-to-learn habit. And the A more complex example would be definitely great. Personally, I prefer mobx-state-tree for setting up a complex state, but it's definitely ok to invent some other approaches. I can imagine the approach of defining separate custom hooks, each exporting the result of The relation between stores would be tough doing as you drafted though. Not sure how to approach that.
Since the docs are using it quite heavily all around, it's indeed considered stable :) |
I am actually thinking that instead of CodeSandbox I would like to include that example in the site itself in a similar fashion the intro page has. Thing is that as much as CodeSandbox is great for experimenting, in a year or so you might decide you need to delete that sandbox for some spring cleaning and it breaks the docs. |
To understand the example code in the 'Accessing stores' recipe, I implemented it in codesandbox. Here is a github repo if you want to fork it: https://github.com/100ideas/mobx-react-usecontext-codesandbox-demo
I was unable to get the original
createStore()
function to work as an observable, so I changed it slightly to anobservable(object)
. You should review it before accepting this PR to make sure what I changed is how you intend this pattern to be implemented.In the original example, the
createStore()
function is never made into an observable (I found this confusing - is it implicit somewhere?), and the function is passed as a reference but not called in the context provider codeReact.useState(createStore)
. I may be missing something implied or obvious about the example code, but I couldn't get it to work as written, so I changed it.