gpt4 book ai didi

javascript - 如何在 React Context 中实现可观察的值监视

转载 作者:行者123 更新时间:2023-12-04 01:10:48 24 4
gpt4 key购买 nike

假设我有一个 Parent组件提供 Context这是一个 Store目的。为简单起见,假设这家商店有一个 和一个函数 更新此值

class Store {
// value

// function updateValue() {}

}

const Parent = () => {
const [rerender, setRerender] = useState(false);
const ctx = new Store();

return (
<SomeContext.Provider value={ctx}>
<Children1 />
<Children2 />
.... // and alot of component here
</SomeContext.Provider>
);
};

const Children1 = () => {
const ctx = useContext(SomeContext);
return (<div>{ctx.value}</div>)
}

const Children2 = () => {
const ctx = useContext(SomeContext);
const onClickBtn = () => {ctx.updateValue('update')}
return (<button onClick={onClickBtn}>Update Value </button>)
}
所以基本上 Children1将显示值,并在 Children2组件,有一个按钮来更新值。
所以我现在的问题是 Children2更新 Store 值,Children1 是 没有重新渲染。 以反射(reflect)新的值(value)。
堆栈溢出的一种解决方案是 here .这个想法是创建一个 stateParent并用它来传递 context给 children 。这将有助于重新渲染 Children1 因为 Parent被重新渲染。
然而,我 不想 Parent重新渲染,因为在 Parent还有很多其他组件。我只要 Children1重新渲染。
那么有没有关于如何解决这个问题的解决方案?我应该使用 RxJS 进行响应式编程还是应该更改代码中的某些内容?谢谢

最佳答案

您可以使用像 redux lib 这样的上下文,如下所示
这很容易使用,以后如果你想转移到 redux,你只需更改存储文件,整个状态管理内容将转移到 redux 或任何其他库。
运行示例:
https://stackblitz.com/edit/reactjs-usecontext-usereducer-state-management
文章:https://rsharma0011.medium.com/state-management-with-react-hooks-and-context-api-2968a5cf5c83
reducer .js

import { combineReducers } from "./Store";

const countReducer = (state = { count: 0 }, action) => {
switch (action.type) {
case "INCREMENT":
return { ...state, count: state.count + 1 };
case "DECREMENT":
return { ...state, count: state.count - 1 };
default:
return state;
}
};

export default combineReducers({ countReducer });
商店.js
import React, { useReducer, createContext, useContext } from "react";

const initialState = {};
const Context = createContext(initialState);

const Provider = ({ children, reducers, ...rest }) => {
const defaultState = reducers(undefined, initialState);
if (defaultState === undefined) {
throw new Error("reducer's should not return undefined");
}
const [state, dispatch] = useReducer(reducers, defaultState);
return (
<Context.Provider value={{ state, dispatch }}>{children}</Context.Provider>
);
};

const combineReducers = reducers => {
const entries = Object.entries(reducers);
return (state = {}, action) => {
return entries.reduce((_state, [key, reducer]) => {
_state[key] = reducer(state[key], action);
return _state;
}, {});
};
};

const Connect = (mapStateToProps, mapDispatchToProps) => {
return WrappedComponent => {
return props => {
const { state, dispatch } = useContext(Context);
let localState = { ...state };
if (mapStateToProps) {
localState = mapStateToProps(state);
}
if (mapDispatchToProps) {
localState = { ...localState, ...mapDispatchToProps(dispatch, state) };
}
return (
<WrappedComponent
{...props}
{...localState}
state={state}
dispatch={dispatch}
/>
);
};
};
};

export { Context, Provider, Connect, combineReducers };
应用程序.js
import React from "react";
import ContextStateManagement from "./ContextStateManagement";
import CounterUseReducer from "./CounterUseReducer";
import reducers from "./Reducers";
import { Provider } from "./Store";

import "./style.css";

export default function App() {
return (
<Provider reducers={reducers}>
<ContextStateManagement />
</Provider>
);
}
组件.js
import React from "react";
import { Connect } from "./Store";

const ContextStateManagement = props => {
return (
<>
<h3>Global Context: {props.count} </h3>
<button onClick={props.increment}>Global Increment</button>
<br />
<br />
<button onClick={props.decrement}>Global Decrement</button>
</>
);
};

const mapStateToProps = ({ countReducer }) => {
return {
count: countReducer.count
};
};

const mapDispatchToProps = dispatch => {
return {
increment: () => dispatch({ type: "INCREMENT" }),
decrement: () => dispatch({ type: "DECREMENT" })
};
};

export default Connect(mapStateToProps, mapDispatchToProps)(
ContextStateManagement
);

关于javascript - 如何在 React Context 中实现可观察的值监视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64886475/

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