Would you all be interested in collaborating on docs with MobX-State-Tree? #445
Replies: 4 comments 8 replies
-
Hi, I use mst-gql and therefore MST in some of my projects, although I'm never sure whether I am doing things "right", so this kind of use case based examples in the documentation would be awesome. One issue, for example, I'm always struggling with is how much logic should I put into MST and how much to leave to components. For example, I always think of my MST store as the "backend" of the application, which handles all the business logic including querying, mutations (graphql in case of mst-gql) and local data manipulations. This way all business logic and state of the app can be tested just by testing the MST store and also, the frontend can be switched easily as it is always calls the MST "backend" as necessary. I wonder, whether this is a good practice or not. |
Beta Was this translation helpful? Give feedback.
-
In general every React query library, naturally, provides hooks for querying/mutations and mst-gql is no exception. This is the basic example it provides: import React from "react"
import { observer } from "mobx-react"
import { Error, Loading, Message } from "./"
import { useQuery } from "../models/reactUtils"
export const Home = observer(() => {
const { store, error, loading, data } = useQuery((store) =>
store.queryMessages()
)
if (error) return <Error>{error.message}</Error>
if (loading) return <Loading />
return (
<ul>
{data.messages.map((message) => (
<Message key={message.id} message={message} />
))}
</ul>
)
}) It conveniently gives you access to the MST store to execute the query, gives loading state, error and finally the data. Now if you image implementing this as an MST action:
If you have multiple query actions then for each you need a separate While all this is doable and straightforward, from DX point of view this is more work than the simplicity of using I guess for PoC or simple prototypes you would just go with |
Beta Was this translation helpful? Give feedback.
-
I'm wondering if anyone here has direct contact with @kitze. It would be really interesting to know how he uses MST in Sizzy. The tree looks huge: https://twitter.com/thekitze/status/1685173949301198848 @jamonholmgren or @mweststrate, perhaps? |
Beta Was this translation helpful? Give feedback.
-
Another issue that may worth mentioning or explaining more in the docs. If you declare the actions like this (version 1): const M = types
.model('MyModel', {})
.actions(self => ({
action1(){},
action2(){}
})) ... you won't get proper IDE introspection, navigation and refactoring support. For example, if you Cmd+Click on node_modules/mobx-state-tree/dist/types/complex-types/model.d.ts
You can also use this (version 2): const M = types
.model('MyModel', {})
.actions(self => {
function action1(){}
function action2(){}
return {
action1,
action2
}
}) This is the worst: Cmd+Click jumps in to the And finally, you can assign the actions to a named variable, which you return (version 3): const M = types
.model('MyModel', {})
.actions(self => {
const actions = {
action1(){},
action2(){}
}
return actions
}) In this case Cmd+Click on This is a huge difference in DX!. Version 1 and 2 are the default suggestions in the docs now: https://mobx-state-tree.js.org/concepts/actions Version 3 is mentioned as a tip: https://mobx-state-tree.js.org/tips/typescript, however, it comes with a note in bold:
I guess this only refers to views, but should be OK for actions and volatile. |
Beta Was this translation helpful? Give feedback.
-
Hey folks!
We are working to improve the MobX-State-Tree docs this year. One major gap is that we don't have a lot of real-world use case examples.
I don't use mst-gql, but I think libraries like yours and mst-query are natural answers to "how do I use MobX-State-Tree in the real world?" since MST offers opinions about how to manage state, but not a lot about how to interface with an external data layer.
I don't mean to presumptuously ask you to do more free work on our behalf, but I'd love to bring the ecosystem together, and if we can offer a space in our docs site for you to showcase this project, I think it would also help show people how they can use GraphQL and MST together for more realistic scenarios.
We'd also be happy to collaborate in other ways if this isn't interesting. Either way, let me know! Happy to chat here, or you can find my email in my GitHub profile if you want to connect individually.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions