From what I could understand, it is necessary to execute some function when the component is ready (charged), utilize useEffect with a array
no position, this means execution after loading the component, example:
function App() {
const [list, setList] = React.useState([
{id: 1, name: 'name 1'},{id: 2, name: 'name 2'},
{id: 3, name: 'name 3'},{id: 4, name: 'name 4'}
]);
function init() {
console.log('carregou ...');
}
React.useEffect(() => {
init();
},[]);
return (
<div>
<ul>
{list && list.map((l,x) => (<li key={x}>{l.name}</li>))}
</ul>
</div>
)
}
ReactDOM.render( <App/> , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
I did not understand very well you have the example of the code in
React
?– novic
My answer came true?
– novic