I'm have difficulties to convert a class component to functional. I have watched multiple examples for doing this task, but still I know the basics. How can I change my code to be a functional component with using useState and useEffect hooks and maybe if someone has some resources where I can find the best practices on this topic? Here is the example of the code that I'm trying to convert:
import React, { Component } from "react";
import { storeProducts, detailProduct } from "./data";
const ProductContext = React.createContext();
//Provider
//Consumer
class ProductProvider extends Component {
state = {
products: [],
detailProduct: detailProduct
};
componentDidMount() {
this.setProducts();
}
setProducts = () => {
let tempProducts = [];
storeProducts.forEach(item => {
const singleItem = {...item};
tempProducts = [...tempProducts, singleItem];
})
this.setState(() => {
return {products: tempProducts }
});
};
handleDetail = () => {
console.log("hello from detail");
};
addToCart = () => {
console.log("hello from addToCart");
};
render() {
return (
<ProductContext.Provider value={{
...this.state,
handleDetail: this.handleDetail,
addToCart: this.addToCart
}}>
{this.props.children}
</ProductContext.Provider>
);
}
}