Im trying to get compare a state from the node express server with the state in the redux store. If the state is different i want to update the store state to the same value as the server state. Now im getting the problem that I can not use these hooks in a non react function. I need this to work but ive looked up the redux documentation and as far as i understand these hooks can only be used in a react function component. Is there another way to still make this work since my whole application is based on the redux state already.
import React from 'react';
import {useDispatch, useSelector} from 'react-redux';
/**
* get the gamestate from the server
* @returns {Promise<{data: any}>}
*/
async function getGamestate() {
const gameStates = await fetch('http://localhost:3000/game-state').then(response => response.json());
return {
data: gameStates,
}
}
export async function CheckIfChanged(){
const serverGameState = await getGamestate();
const clientGameState = useSelector(state => state.gameState);
if(serverGameState.data !== clientGameState){
console.log(serverGameState)
//UpdateGameState(serverGameState)
}else {
console.log("still the same")
}
}
Update: Im planning to call the function here in the mainview, this is basically a wrapper thats used in the whole application. The checkIfChanged function will be called every 5 seconds or so.
import React from 'react';
import '../../style/App.scss';
import Wrapper from '../wrapper/wrapper';
import StartView from '../startview/StartView';
import { useSelector } from 'react-redux';
function MainView(){
const gameState = useSelector(state => state.gameState);
//arround here i would call it and will be updating every 5 seconds later
checkIfChanged();
switch (gameState) {
case "notStarted":
return (
<StartView/>
);
case "video":
case "keypad":
case "puzzle":
case "completed":
case "failed":
return (
<Wrapper/>
);
default:
return (
<div className="contentArea">
<h1>Er is een fout opgetreden</h1>
</div>
);
}
}
export default MainView;