question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature request: option for useStore.subscribe fire once on initialisation

See original GitHub issue

Currently useStore.subscribe only fires when the subscribed part of the store changes. It would be useful if there was an option to force the function to fire once upon initialisation. Maybe I’m missing something, here’s an example:

useEffect(() => {
    // I don't want to have to repeat myself up here just for initialisation
    const { position, rotation } = getSceneObject(props.id)(useStore.getState())
    target.current.position.set(position[0], position[1] - 3, position[2])
    plane.current.position.set(...position)
    plane.current.rotation.set(rotation[0], Math.PI, 0)

    return useStore.subscribe(
      ({ position, rotation }: SceneObject) => {
        target.current.position.set(position[0], position[1] - 3, position[2])
        plane.current.position.set(...position)
        plane.current.rotation.set(rotation[0], Math.PI, 0)
      },
      (state) => getSceneObject(props.id)(state)
    )
  }, [props.id])

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
timothyallancommented, Oct 1, 2021

Just dropping in my 2c here to think about for potential future dev: I use this feature all the time in MobX. They have optional params you can pass into the subscribe function, one is called fireImmediately, which does exactly what it sounds like.

0reactions
dai-shicommented, Oct 21, 2021

I will try this feature (fireImmediately option) along with #555. It will be a new middleware.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recipes - Zustand Documentation - Pmndrs.docs
For more control over re-rendering, you may provide an alternative equality ... fires on every change const unsub1 = useStore.subscribe(console.log) ...
Read more >
Subscribe fires twice when I revisit a page - angular
When you revisit it will create an instance of that service and initialize all the subscription once again because you have injected via ......
Read more >
Zustand | The State of State Management in React - Morioh
It may be the one state-manager in the React space that gets all of these right. ... fire immediately const unsub5 = useStore.subscribe(state...
Read more >
QRadar APARs 101 - IBM
The hostcontext service fails to initialize after the appliance reboot. ... optimize option can be required for proper QRadar performance and functionality.
Read more >
Hooks API Reference - React
They let you use state and other React features without writing a class. ... The default behavior for effects is to fire the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found