Reputation: 141
I have a lot of methods in my functional component which i would like to place in separate files,these methods use the state of the functional component and it should be able to set the states.Example below:
export default functional_component(){
const [state, setState] = React.useState({
//state variables
})
}
//another file.js
const foo = () =>{
setState();
}
Upvotes: 0
Views: 178
Reputation: 810
You can pass setState function as parameter to your functions in other files though it does not really make sense to seperate state logic of component to seperate script files unless you have a very good reason to do it.
export default functional_component(){
const [state, setState] = React.useState({
//state variables
foo(setState)
})
}
//another file.js
const foo = (setState) =>{
setState();
}
Upvotes: 1