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.

Differences between `computed` and a hook

See original GitHub issue

What are the differences between using computed:

const model = {
  user: null,
  isLoggedIn: computed((state) => state.user !== null),
};

// In component
const isLoggedIn = useStoreState(state => state.model.isLoggedIn)

And a hook like this:

function useIsLoggedIn(){
  const user = useStoreState(state => state.model.user)
  return user !== null
}

// In component
const isLoggedIn = useIsLoggedIn()

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
jmyrlandcommented, Oct 8, 2022

This is a hook, so it can’t be used inside actions and thunks. On the other hand, if I make it a computed property, then I can use it within actions and thunks. But only the cached value will be available, and I feel like that could introduce hard-to-detect bugs.

Well it might be in some cases - and it all depends on your store. I would suggest you to test out if computed props are working in your case. If it works - great, if it doesn’t, you can get around it via the solution below.

Is there another approach?

Computed props are just derived of state. If you are worried that you might get cached computed props, you could extrapolate the “compute” generator and reuse it within your action/thunk to regenerate the data based on the current state.

const model = {
  comptutedProp: computed((state) => generateComplexData(state))
  myAction: action((state) => {
    const data = generateComplexData(state)
    // etc..
  })
}

const generateComplexData = (state) => { /* extract from state */ }
0reactions
jmyrlandcommented, Oct 8, 2022

How does it determine if the input state has changed? If the input state is an object, will the input always be considered “changed” and cause excessive re-renders?

The input is deep-diffed based on the previous previous input.

The computed property is also checked and only updated if it has changed, to prevent excessive renders.

see #764 for more details

I’m able to pass a custom equality function to useStoreState, but not to computed.

State resolvers provides the input to the computed props. Equality checks are handled internally.

Read about state resolvers for computed props here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the Difference Between the Created and Mounted ...
The created() hook is used for fetching data from backend API and setting it to data properties. But in SSR, the mounted hook...
Read more >
watch vs. watchEffect when to use what with Vue.js
The watchEffect() hook works like the computed() hook or the computed option, but instead of returning a value, you use it to trigger...
Read more >
Computed Properties - Vue.js
Think of a computed property as declaratively describing how to derive a value based on other values - its only responsibility should be...
Read more >
Methods, Computed, and Watchers in Vue.js | CSS-Tricks
In Vue, we use data to track changes to a particular property that we'd like to be reactive. Computed properties allow us to...
Read more >
Vue JS - Differences between Computed and Watch
While computed properties are more appropriate in most cases, watchers are more powerful and allow us to perform complex operations such as HTTP ......
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