aboutsummaryrefslogblamecommitdiff
path: root/src/unstated-next.tsx
blob: e30d20562407b8782b9136bdd510c05b9a225b25 (plain) (tree)
1
2
3
4
5
6
7
8
9

                         

                                                       


                                 

                                                                    


                                 


                                                     

                                                             

                                                                 













                                                                                              


                                                  

                                       
import React from "react"

export interface ContainerProviderProps<State = void> {
	initialState?: State
	children: React.ReactNode
}

export interface Container<Value, State = void> {
	Provider: React.ComponentType<ContainerProviderProps<State>>
	useContainer: () => Value
}

export function createContainer<Value, State = void>(
	useHook: (initialState?: State) => Value,
): Container<Value, State> {
	let Context = React.createContext<Value | null>(null)

	function Provider(props: ContainerProviderProps<State>) {
		let value = useHook(props.initialState)
		return <Context.Provider value={value}>{props.children}</Context.Provider>
	}

	function useContainer(): Value {
		let value = React.useContext(Context)
		if (value === null) {
			throw new Error("Component must be wrapped with <Container.Provider>")
		}
		return value
	}

	return { Provider, useContainer }
}

export function useContainer<Value, State = void>(
	container: Container<Value, State>,
): Value {
	return container.useContainer()
}