useContentcreateContext

通过根组件定义全局状态

context提供了一个强大的机制,让react应用具备全局的响应式数据能力。

  • 方便不同层级之间共享数据
  • 但是难以追踪context的改变来源
  • 组件复用变得困难,因为需要父组件链上有contextProvider组件

适用的场景:themelanguage

// 通过React.createContext创建context
const initialValue = {}
export const CustomContext = React.createContext(initialValue)
export const App = () => {
	return (
		<CustomContext.Provider value = {{/** 当前store的值*/}}>
			<Child />
		</CustomContext.Provider>
	)
}
 
const Child = () => {
	const someThing = useContent(CustomContext)
	return (
		<div>{someThing.xxx}</div>
	)
}