-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Skeleton for store #33215
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
base: main
Are you sure you want to change the base?
Skeleton for store #33215
Conversation
Comparing: 4448b18...4a4147b Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
let assertLog; | ||
let createStore; | ||
|
||
describe('ReactUse', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReactUseStore?
export function createStore<T>( | ||
defaultValue: T, | ||
reducer?: (T, mixed) => T, | ||
): ReactStore<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be able have strong types for reducer actions while keeping both reducers and actions optional.
export function createStore<T>( | |
defaultValue: T, | |
reducer?: (T, mixed) => T, | |
): ReactStore<T> { | |
export function createStore<Value, Action = empty>( | |
defaultValue: T, | |
reducer?: (value: T, action?: Action) => T, | |
): ReactStore<T, Action> { |
const store: ReactStore<T> = { | ||
$$typeof: REACT_STORE_TYPE, | ||
|
||
_current: defaultValue, | ||
_sync: defaultValue, | ||
_transition: defaultValue, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const store: ReactStore<T> = { | |
$$typeof: REACT_STORE_TYPE, | |
_current: defaultValue, | |
_sync: defaultValue, | |
_transition: defaultValue, | |
}; | |
const store: ReactStore<Value, mixed> = { | |
$$typeof: REACT_STORE_TYPE, | |
_current: defaultValue, | |
_sync: defaultValue, | |
_transition: defaultValue, | |
}; |
export type ReactStore<T> = { | ||
$$typeof: symbol, | ||
_current: T, | ||
_sync: T, | ||
_transition: T, | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if the Action
type makes sense anymore for this PR.
But the +
should make the value co-variant, (making it possible to Store<Dog>
where a Store<Animal>
is needed)
While the -
makes Action
contra-variant (allowing you to pass a Dog
when an Animal
is accepted as/within an Action
.)
export type ReactStore<T> = { | |
$$typeof: symbol, | |
_current: T, | |
_sync: T, | |
_transition: T, | |
}; | |
export type ReactStore<+Value, -Action = empty> = { | |
$$typeof: symbol, | |
_current: Value, | |
_sync: Value, | |
_transition: Value, | |
}; |
Would love support for asyncIterables some day. |
Note: This doesn't do anything yet. Just sets up some of the skeleton, feature flags, and test files.
Conceptually there's an API to create a store, and then you can read the value with
use(store)
.For more info on our plans for this API, check out the React Labs post: