Skip to content

initState in Provider becomes required with an Array of Arguments. #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ import { createContainer } from "../src/unstated-next"
import { render } from "react-dom"

function useCounter(initialState = 0) {
let [count, setCount] = useState(initialState)
let decrement = () => setCount(count - 1)
let increment = () => setCount(count + 1)
const [count, setCount] = useState(initialState)
const decrement = () => setCount(count - 1)
const increment = () => setCount(count + 1)
return { count, decrement, increment }
}

let Counter = createContainer(useCounter)
const Counter = createContainer(useCounter)

function useRequiredCounter(step: number) {
const { count } = Counter.useContainer()
const computed = count + step
return { count, step, computed }
}

const RequiredCounter = createContainer(useRequiredCounter)

function CounterDisplay() {
let counter = Counter.useContainer()
const counter = Counter.useContainer()
return (
<div>
<button onClick={counter.decrement}>-</button>
Expand All @@ -22,14 +30,26 @@ function CounterDisplay() {
)
}

function RequiredCounterDisplay() {
const { count, step, computed } = RequiredCounter.useContainer()
return (
<div>
Computed Value With Step {step}: {count} + {step} = {computed}
</div>
)
}

function App() {
return (
<Counter.Provider>
<Counter.Provider initialState={[]}>
<CounterDisplay />
<Counter.Provider initialState={2}>
<Counter.Provider initialState={[2]}>
<div>
<div>
<CounterDisplay />
<RequiredCounter.Provider initialState={[2]}>
<RequiredCounterDisplay />
</RequiredCounter.Provider>
</div>
</div>
</Counter.Provider>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unstated-next",
"version": "1.1.0",
"version": "2.0.0",
"description": "200 bytes to never think about React state management libraries ever again",
"source": "src/unstated-next.tsx",
"main": "dist/unstated-next.js",
Expand Down
25 changes: 14 additions & 11 deletions src/unstated-next.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ import React from "react"

const EMPTY: unique symbol = Symbol()

export interface ContainerProviderProps<State = void> {
initialState?: State
children: React.ReactNode
}
export type ContainerProviderProps<
State extends any[]
> = React.PropsWithChildren<{
initialState: State
}>

export type UseHookFn<Value, State extends any[]> = (...args: State) => Value

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

export function createContainer<Value, State = void>(
useHook: (initialState?: State) => Value,
export function createContainer<Value, State extends any[]>(
useHook: UseHookFn<Value, State>,
): Container<Value, State> {
let Context = React.createContext<Value | typeof EMPTY>(EMPTY)

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

function useContainer(): Value {
Expand All @@ -33,7 +36,7 @@ export function createContainer<Value, State = void>(
return { Provider, useContainer }
}

export function useContainer<Value, State = void>(
export function useContainer<Value, State extends any[]>(
container: Container<Value, State>,
): Value {
return container.useContainer()
Expand Down