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.

Is there any way to change states outside component?

See original GitHub issue

Hello, thank you for the amazing library. I just have one question: Is there any way to change state(s) from outside of a component?

I mean, now we do this to change states inside a component:

const Test: React.FC<Props> = (props) => {
    const {counter} = useActions()
    const {addCounter} = useAppState()

    return <Button onClick={()=> {
        addCounter(1)
    }}>{counter}</Button>

}

export default Test;

And to change outside of a component, I do this:

// In a different file
export class SomeClass {
    static changeCounter(addCounter) {
        addCounter(1)
    }
}

// in Test.tsx
const Test: React.FC<Props> = (props) => {
    const {counter} = useActions()
    const {addCounter} = useAppState()

    return <Button onClick={()=> {
        SomeClass.changeCounter(addCounter)
    }}>{counter}</Button>

}

export default Test;

I pass the actions down to the functions and then change states. Its okay, until there’s just one action to pass. But if there’s multiple actions to pass down through functions, the code looks really messy.

Is there any way, we can change states without needing to pass it down through a component through a function parameter?

Thanks @christianalfoni

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
grandevelcommented, Oct 12, 2021

Closing this as it looks resolved, but please pick back up in the discord channel if more assistance needed.

1reaction
henri-hulskicommented, Sep 3, 2021

@p32929 Hi! Maybe you should asked this question on the discord channel. I think it belongs there as it’s a support question, not an issue. And there’re much more folks out there which can help you. Christian also frequently answers questions over there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set React component state from outside of component
By definition, state is not accessible from outside the component. And always copying props to state is not recommended.
Read more >
How to update a component's state from outside of a React app
In some circumstances, you'll want to update a component's state or access its methods from outside of a React app.
Read more >
Talk to React components from outside React | simbathesailor
We want to show the modal, then and there , when some condition passes. That condition can be in a non-react codebase. Let...
Read more >
Accessing React Component Methods and State from Outside ...
You can use the CSS from another Pen by using its URL and the proper URL extension.
Read more >
React setState usage and gotchas - ITNEXT
setState () enqueues changes to the component state and tells React that this component and its children need to be re-rendered with 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