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.

Update state in a function

See original GitHub issue

This should be easy, so I’m probably missing something. I know how to update the state using the useStore hook inside a React component, but how do I update the store inside a functional component?

export function handleDeepLink(event) {
  const setUser = useStore((state) => state.setUser); << // Not allowed

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
dai-shicommented, Oct 30, 2020

how do I update the store inside a functional component?

So, you want to update the store outside a component, correct?

Here’s what you want:

export function handleDeepLink(event) {
  const setUser = useStore.getState().setUser;
1reaction
dai-shicommented, Feb 19, 2021

@anastely You can add them inside too. It’s working because they never change in your use case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the State Hook - React
What does useState return? It returns a pair of values: the current state and a function that updates it. This is why we...
Read more >
Updating a state inside a React function - Stack Overflow
import React, { Component } from 'react'; class App extends Component { state = { data:'AA', newData: 'AA AA' } newData1=()=>{this.state.data + ...
Read more >
React - Normal State Update vs Functional State Update
If we pass a function to React's useState(), it would call that function and use the return value to initialize the state. import...
Read more >
useState in React: A complete guide - LogRocket Blog
React Hooks: Update state ... The second element returned by useState is a function that takes a new value to update the state...
Read more >
How to Use State in Functional React Components
The state update function is just a regular function. It's used in the onClick handler to replace the current state value. React's internal ......
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