Skip to content Skip to sidebar Skip to footer

Externalise Common Functions In Various React Components

In different components where I use the same functions when it comes to styling or something else. Some of these functions use this.setState({...}); I want to collect all these fun

Solution 1:

I assume you mean exporting one function and call this function in various components while remaining the scope of this

MyExportedFunctions.js:

exportfunctionhandleChange(value, {target: {name, type}}) {
    this.setState({[name]: value}, () =>console.log(this.state));
}

MyComponent.js:

import {handleChange} from "./MyExportedFunctions";

        classMyComponentextendsComponent{
            constructor(props) {
                super(props);
                this.handleChange = handleChange.bind(this);

                this.state = {

                };
            }
            ...
     }

Did you mean something like this?

Post a Comment for "Externalise Common Functions In Various React Components"