乐闻世界logo
搜索文章和话题

How to set zustand state in a class component

1个答案

1

When using class components, we cannot directly use Zustand's hooks because hooks are designed for functional components. However, we can make class components access Zustand state through alternative methods. A common approach is to use Higher-Order Components (HOC) or the subscription pattern to access and update the state.

Approach One: Using Higher-Order Components (HOC)

We can create a Higher-Order Component (HOC) to wrap our class component, enabling us to use Zustand's hooks within the HOC to access and manipulate the state, and then pass these states to the class component through props.

Assume we have a Zustand store defined as follows:

javascript
import create from 'zustand'; const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })), decrement: () => set(state => ({ count: state.count - 1 })) }));

Then we create a Higher-Order Component:

javascript
import React from 'react'; import useStore from './store'; // Import our store const withStore = (Component) => { return function EnhancedComponent(props) { const { count, increment, decrement } = useStore(); return <Component {...props} count={count} increment={increment} decrement={decrement} />; }; }; export default withStore;

Now, we can use this Higher-Order Component to wrap our class component:

javascript
import React, { Component } from 'react'; import withStore from './withStore'; // Import the HOC class Counter extends Component { render() { const { count, increment, decrement } = this.props; return ( <div> <h1>{count}</h1> <button onClick={increment}>Increase</button> <button onClick={decrement}>Decrease</button> </div> ); } } export default withStore(Counter); // Wrap the class component with HOC

Approach Two: Using Subscription Pattern

If you prefer not to use HOC, you can directly use Zustand's subscription pattern within the class component. This approach requires manually handling subscription and unsubscription within the component's lifecycle.

Here's how to use Zustand subscription in a class component:

javascript
import React, { Component } from 'react'; import useStore from './store'; // Import store class Counter extends Component { state = { count: useStore.getState().count // Initial state }; componentDidMount() { this.unsubscribe = useStore.subscribe(count => { this.setState({ count }); }, state => state.count); } componentWillUnmount() { this.unsubscribe(); } increment = () => { useStore.getState().increment(); } decrement = () => { useStore.getState().decrement(); } render() { const { count } = this.state; return ( <div> <h1>{count}</h1> <button onClick={this.increment}>Increase</button> <button onClick={this.decrement}>Decrease</button> </div> ); } } export default Counter;

In this example, we subscribe to state changes when the component mounts and unsubscribe when it unmounts, ensuring the component state remains synchronized with the Zustand store.

2024年8月1日 09:42 回复

你的答案