Is there any way to change states outside component?
See original GitHub issueHello, 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:
- Created 2 years ago
- Comments:10 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closing this as it looks resolved, but please pick back up in the discord channel if more assistance needed.
@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.