GMOD / jbrowse-react-app-nextjs-demo

1 stars 0 forks source link

Question: jbrowse2 state store #2

Open alpapan opened 5 months ago

alpapan commented 5 months ago

Good day Colin

Not sure the better way to ask you this but i reckon an issue on this repo is not the worst idea.

For my sins, I'm making a web app using nextjs that brings together data from APIs and passes them on to front end tools.

So i'm trying to think of how to manage state in react in a more generic way and i was thinking of using valtio.

I gave it a shot (with jbrowse) but i didn't get very far and not sure if it's even a good idea/feasible.

Would you have any experience or recommendations?

thank you, alexie

cmdcolin commented 5 months ago

that's an interesting question. i could probably write a small essay on it. some notes

cmdcolin commented 5 months ago

all that said, if you have any code samples you want me to look at let me know :) note that jbrowse isn't really able to take advantage of any of the 'server side rendering' aspects of nextjs

alpapan commented 4 months ago

thank you

The use case I'm trying to explore is aligning the output of a separate graphing component (or more) with info from the jb view (dynamically, without UI interaction) and thought the best way would be to populate a global store. So with mbox i could just set whatever shared data with makeObservable?

What would be the effect of the jb2 component having an mbox-state object? will it be hierarchally integrated to a parent document's (the app's) mbox-state object? sorry if this is a dumb question

cmdcolin commented 4 months ago

with jbrowse, sometimes it is best to use the "observer" concept. here are two examples we have

this one observes the "currently visible region" and just prints it out (as well as the "static blocks", which is just a rendering thing we do)

live link https://jbrowse.org/storybook/lgv/main/?path=/story/source-code-for-examples--with-observe-visible-regions source code https://github.com/GMOD/jbrowse-components/blob/main/products/jbrowse-react-linear-genome-view/stories/examples/WithObserveVisibleRegions.tsx

this one fetches all the features. it's a bit involved as it actually has to request the features in your code https://jbrowse.org/storybook/lgv/main/?path=/story/source-code-for-examples--with-observe-visible-features source code https://github.com/GMOD/jbrowse-components/blob/main/products/jbrowse-react-linear-genome-view/stories/examples/WithObserveVisibleFeatures.tsx

the secret is making your components wrapped in an "observer". so this component for example https://github.com/GMOD/jbrowse-components/blob/main/products/jbrowse-react-linear-genome-view/stories/examples/WithObserveVisibleRegions.tsx#L16-L24

it is automatically re-rendered when the things that it accesses during rendering change, because it is wrapped in the mobx-react "observer" function (sometimes called "HOC" in react terms, it just wraps the function component)

those examples sort of show how you can start to try to "react to what jbrowse is showing"

i could try to anwer the other questions but it's possible the above links could be a start