I have this functional/stateless component where I have a function filterByCardinalPoint
which I need to call when component is mounted.
Is there a way to do that without writing the component as a class?
const PassengersCircle = ({ navigationStore, passengersData }) => {
const filterByCardinalPoint = () => {
const passengersByCardinalPoint = filter('value');
...
};
return (...)
};
You can use the useEffect
hook to run a function when the component has been mounted. By giving it an empty array as second argument it will only be run after the initial render.
const PassengersCircle = ({ navigationStore, passengersData }) => {
const filterByCardinalPoint = () => {
const passengersByCardinalPoint = filter('value');
// ...
};
useEffect(() => {
filterByCardinalPoint();
}, []);
return <>{/* ... */}</>;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With