Skip to content

Instantly share code, notes, and snippets.

@Codennnn
Last active March 19, 2022 11:41
Show Gist options
  • Save Codennnn/2df3bfc97883390e0b29acc07708ea21 to your computer and use it in GitHub Desktop.
Save Codennnn/2df3bfc97883390e0b29acc07708ea21 to your computer and use it in GitHub Desktop.
react context 的数据状态管理
import { createContext, useContext, useEffect, useState } from 'react';
const XxxContext = createContext({});
export function XxxContextProvider(props: { children: React.ReactNode }) {
const [state, setState] = useState();
const value = { state, setState };
return <XxxContext.Provider value={value}>{props.children}</XxxContext.Provider>;
}
export function useXxxContext() {
const context = useContext(XxxContext);
return context;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment