Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Skeleton for store #33215

wants to merge 1 commit into from

Conversation

rickhanlonii
Copy link
Member

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:

Screenshot 2025-05-15 at 12 53 34 PM

@github-actions github-actions bot added the React Core Team Opened by a member of the React Core Team label May 15, 2025
@react-sizebot
Copy link

react-sizebot commented May 15, 2025

Comparing: 4448b18...4a4147b

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 529.74 kB 529.74 kB = 93.49 kB 93.49 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 651.48 kB 651.48 kB = 114.76 kB 114.76 kB
facebook-www/ReactDOM-prod.classic.js +0.03% 675.72 kB 675.91 kB +0.03% 118.85 kB 118.88 kB
facebook-www/ReactDOM-prod.modern.js +0.03% 666.00 kB 666.19 kB +0.03% 117.23 kB 117.27 kB
facebook-www/React-prod.modern.js +2.41% 21.01 kB 21.52 kB +2.01% 5.37 kB 5.47 kB
facebook-www/React-prod.classic.js +2.41% 21.01 kB 21.52 kB +2.03% 5.37 kB 5.47 kB
facebook-www/React-profiling.modern.js +2.36% 21.45 kB 21.95 kB +1.98% 5.45 kB 5.56 kB
facebook-www/React-profiling.classic.js +2.36% 21.45 kB 21.96 kB +1.98% 5.45 kB 5.56 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
facebook-www/React-prod.modern.js +2.41% 21.01 kB 21.52 kB +2.01% 5.37 kB 5.47 kB
facebook-www/React-prod.classic.js +2.41% 21.01 kB 21.52 kB +2.03% 5.37 kB 5.47 kB
facebook-www/React-profiling.modern.js +2.36% 21.45 kB 21.95 kB +1.98% 5.45 kB 5.56 kB
facebook-www/React-profiling.classic.js +2.36% 21.45 kB 21.96 kB +1.98% 5.45 kB 5.56 kB
facebook-www/React-dev.modern.js +0.84% 56.14 kB 56.62 kB +0.80% 12.31 kB 12.40 kB
facebook-www/React-dev.classic.js +0.84% 56.15 kB 56.62 kB +0.80% 12.31 kB 12.41 kB
facebook-react-native/react/cjs/React-prod.js +0.20% 19.09 kB 19.13 kB +0.16% 4.95 kB 4.96 kB

Generated by 🚫 dangerJS against 4a4147b

let assertLog;
let createStore;

describe('ReactUse', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactUseStore?

Comment on lines +14 to +17
export function createStore<T>(
defaultValue: T,
reducer?: (T, mixed) => T,
): ReactStore<T> {
Copy link
Contributor

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.

Suggested change
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> {

Comment on lines +22 to +28
const store: ReactStore<T> = {
$$typeof: REACT_STORE_TYPE,

_current: defaultValue,
_sync: defaultValue,
_transition: defaultValue,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
};

Comment on lines +67 to +73
export type ReactStore<T> = {
$$typeof: symbol,
_current: T,
_sync: T,
_transition: T,
};

Copy link
Contributor

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.)

Suggested change
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,
};

@nmn
Copy link
Contributor

nmn commented May 22, 2025

For more info on our plans for this API

Would love support for asyncIterables some day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants