How to save a function instead of a value in RTK so that i can save a function reference in a component and call it in another component in React?
I just want to reset timer from another component. I don't know what to do so i made a state in react for that function reference
HOME COMPONENT
import React, { useEffect } from "react";
import { Outlet } from "react-router-dom";
import { useSelector,useDispatch } from "react-redux";
import Sidebar from "../components/Sidebar/Sidebar";
import { useIdleTimer } from "react-idle-timer";
import { userLogout } from "../utils/utils";
import LoadingAnimation from "../components/LoadingAnimation/LoadingAnimation";
import { setLogoutTimerReset } from "../store/slicers/logoutSlice";
const Home = () => {
const { user } = useSelector((state) => state.auth);
const dispatch = useDispatch();
const onIdle = () => {
userLogout();
};
const { reset } = useIdleTimer({
onIdle,
timeout: 60 * 30 * 1000, //60 minute idle timeout
// timeout: 30000_000,
});
useEffect(() => {
dispatch(setLogoutTimerReset(reset));
return () => {};
}, []);
return user ? (
<section className="home-sidebar-flex">
<Sidebar />
<main className="map-main-parent--container">
<Outlet />
</main>
</section>
) : (
<LoadingAnimation />
);
};
export default Home;
SOME OTHER COMPONENT
import React from 'react'
import { useSelector } from "react-redux";
function App() {
const { resetLogoutTimer } = useSelector((state) => state.logoutState);
return (
<div onClick={resetLogoutTimer}>Resetter</div>
)
}
export default App
SLICE CODE
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
resetLogoutTimer: () => {},
};
export const logoutSlice = createSlice({
name: "logout",
initialState,
reducers: {
setLogoutTimerReset: (state, payload) => {
state.resetLogoutTimer = payload;
},
},
});
export const { setLogoutTimerReset } = logoutSlice.actions;
export default logoutSlice.reducer;
Reset idleTime from another component I am using react-idle-timer