I do not understand the need to use useEffect for cases where I want to do things, for example, before rendering. For example, in the code below, I want to fill the data with information, for example, by receiving from an API. Now the difference of the following code:
const Product_List = (props) => {
const [data, setData] = useState();
getData((response)=> setData(response));
return(
<FlatList
data={data}
/>
)
}
With the following code:
const Product_List = (props) => {
const [data, setData] = useState();
useEffect(() => {
getData((response)=> setData(response));
}, []);
return(
<FlatList
data={data}
/>
)
}
In other words, different of this code:
class Product_List extends Component{
constructor(props) {
this.state = {
data: []
}
getData((response)=> this.setState({data: response}));
}
render(){
return(
<FlatList
data={data}
/>
)
}
}
With the following code:
class Product_List extends Component{
constructor(props) {
this.state = {
data: []
}
}
componentDidMount(){
getData((response)=> this.setState({data: response}));
}
render(){
return(
<FlatList
data={data}
/>
)
}
}