gpt4 book ai didi

reactjs - 非 react 功能中的 Redux

转载 作者:行者123 更新时间:2023-12-05 03:53:02 26 4
gpt4 key购买 nike

我正在尝试比较来自 Node Express 服务器的状态与 Redux 存储中的状态。如果状态不同,我想将商店状态更新为与服务器状态相同的值。现在我遇到了无法在非 react 函数中使用这些 Hook 的问题。我需要这个才能工作,但我查阅了 redux 文档,据我所知,这些 Hook 只能用于 react 功能组件。因为我的整个应用程序已经基于 redux 状态,所以还有另一种方法可以使这项工作正常进行吗?

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")
}
}

更新:我打算在主视图中调用该函数,这基本上是整个应用程序中使用的包装器。 checkIfChanged 函数将每 5 秒左右调用一次。

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;

最佳答案

你不能直接在 render 中定义带钩子(Hook)的异步方法。但是,您可以在自定义 Hook 中转换您的函数,然后可以使用 useSelector 并实现 useEffect 来同步您的更改

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 function useCheckIfChanged(){ // not an async function
const clientGameState = useSelector(state => state.gameState);
const clientGameStateRef = useRef(clientGameState);
// Using a ref since we can't add clientGameState as a dependency to useEffect and it is bounded by closure

useEffect(() =-> {
clientGameStateRef.current = clientGameState;
}, [clientGameState]);

useEffect(() => {
setInterval(async() => {
const serverGameState = await getGamestate();
// value inside here for clientGameState will refer to the original state only and hence we are using a ref which we update in another useEffect
if(serverGameState.data !== clientGameStateRef.current){
console.log(serverGameState)
//UpdateGameState(serverGameState)

}else {
console.log("still the same")
}
}, 5000)
}, []);

}

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);
useCheckIfChanged(); // using the custom hook here

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;

关于reactjs - 非 react 功能中的 Redux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61888046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com