I have this code in my React component.
What I need is to reuse some components and function from other files.
But some functions update my state.
I wanna know what is the best practice to accomplish this.
MyComponent primary:
import React, { Component, Fragment } from "react";
import { GenericFunction, GenericComponent } from "./functions.js";
class MyComponent extends Component {
state = {
myValueState: false
};
render() {
const { myValueState } = this.state;
return (
<div>
<GenericComponent
GenericFunction={GenericFunction}
// GenericFunction={GenericFunction.bind(this)} // -----> With this it works good, but is this a best practice?
/>
</div>
);
}
}
export default MyComponent;
functions.js file:
export function GenericFunction(items) {
if (items) {
this.setState({ ...this.state, myValueState: true });
} else {
this.setState({ ...this.state, myValueState: false });
}
}
GenericFunction={GenericFunction.bind(this)}
is this a good way?
I have heard about problem with bind(this)
. Am I wrong?