I want to execute a dispatch called loadMe once when rendering for the first time with useEffect in the About component.
However, when the screen is refreshed, the dispatch is executed 4 times. Like the picture blow
How can I run it only once?
this is my code
About.tsx
const About = () => {
const dispatch = useAppDispatch();
useEffect( () => {
const loadMy = async()=> {
dispatch(loadMe());
}
loadMy();
},[]);
return (
<>
<div>About page</div>
</>
);
};
export default About;
Action.tsx
export const loadMe = createAsyncThunk(
'/loadMe',
async ( data, {rejectWithValue}) => {
try {
const userId = await localStorage.getItem('UserId');
return userId;
} catch (error: any) {
console.error(error);
return rejectWithValue(error.response.data);
}
},
);