我在一个网站上工作,该网站使用zustand在文件中存储了一段全局状态。我需要能够在类组件中设置该状态。我可以使用钩子在函数组件中设置状态,但我想知道是否有一种方法可以在类组件中使用zustand。
如果有用的话,我已经为这个问题创建了一个沙箱:https://codesandbox.io/s/crazy-darkness-0ttzd
这里我在一个功能组件中设置状态:
function MyFunction() {
const { setPink } = useStore();
return (
<div>
<button onClick={setPink}>Set State Function</button>
</div>
);
}我的状态存储在这里:
export const useStore = create((set) => ({
isPink: false,
setPink: () => set((state) => ({ isPink: !state.isPink }))
}));如何在类组件中设置状态?:
class MyClass extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div>
<button
onClick={
{
/* setPink */
}
}
>
Set State Class
</button>
</div>
);
}
}发布于 2021-02-07 15:30:42
类组件与钩子最相似的是高阶组件(HOC)模式。让我们将钩子useStore转换为即席withStore。
const withStore = BaseComponent => props => {
const store = useStore();
return <BaseComponent {...props} store={store} />;
};我们可以在包装在withStore中的任何类组件中作为道具访问存储。
class BaseMyClass extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
const { setPink } = this.props.store;
return (
<div>
<button onClick={setPink}>
Set State Class
</button>
</div>
);
}
}
const MyClass = withStore(BaseMyClass);发布于 2021-02-07 14:30:43
它似乎使用了钩子,所以在类中你可以使用实例:
import { useStore } from "./store";
class MyClass extends Component {
render() {
return (
<div>
<button
onClick={() => {
useStore.setState({ isPink: true });
}}
>
Set State Class
</button>
</div>
);
}
}
发布于 2021-02-07 14:30:43
创建一个功能组件和基于类的组件都可以使用的React上下文提供程序。将useStore挂钩/状态移动到上下文提供程序。
store.js
import { createContext } from "react";
import create from "zustand";
export const ZustandContext = createContext({
isPink: false,
setPink: () => {}
});
export const useStore = create((set) => ({
isPink: false,
setPink: () => set((state) => ({ isPink: !state.isPink }))
}));
export const ZustandProvider = ({ children }) => {
const { isPink, setPink } = useStore();
return (
<ZustandContext.Provider
value={{
isPink,
setPink
}}
>
{children}
</ZustandContext.Provider>
);
};index.js
用ZustandProvider组件包装应用程序。
...
import { ZustandProvider } from "./store";
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(
<StrictMode>
<ZustandProvider>
<App />
</ZustandProvider>
</StrictMode>,
rootElement
);在两个组件中使用ZustandContext上下文
MyFunction.js
import React, { useContext } from "react";
import { ZustandContext } from './store';
function MyFunction() {
const { setPink } = useContext(ZustandContext);
return (
<div>
<button onClick={setPink}>Set State Function</button>
</div>
);
}MyClass.js
import React, { Component } from "react";
import { ZustandContext } from './store';
class MyClass extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div>
<button
onClick={this.context.setPink}
>
Set State Class
</button>
</div>
);
}
}
MyClass.contextType = ZustandContext;在App中换入新的ZustandContext,而不是直接使用useStore挂钩。
import { useContext} from 'react';
import "./styles.css";
import MyClass from "./MyClass";
import MyFunction from "./MyFunction";
import { ZustandContext } from './store';
export default function App() {
const { isPink } = useContext(ZustandContext);
return (
<div
className="App"
style={{
backgroundColor: isPink ? "pink" : "teal"
}}
>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<MyClass />
<MyFunction />
</div>
);
}
如果您不能在MyClass组件上设置任何特定的上下文,您可以使用ZustandContext.Consumer来提供setPink回调作为工具。
<ZustandContext.Consumer>
{({ setPink }) => <MyClass setPink={setPink} />}
</ZustandContext.Consumer>MyClass
<button onClick={this.props.setPink}>Set State Class</button>https://stackoverflow.com/questions/66084662
复制相似问题