-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
refactor(react): optimize react layer #5000
Conversation
TkDodo
commented
Jul 27, 2024
- replace use-sync-external-store/shim with useSyncExternalStore from react
- do not memoize getSnapshot in uSES
- implement getServerSnapshot in uSES
- expect store to always be defined
- update react types to v18 and testing library to v16
- replace use-sync-external-store/shim with useSyncExternalStore from react - do not memoize getSnapshot in uSES - implement getServerSnapshot in uSES - expect store to always be defined - update react types to v18 and testing library to v16
🦋 Changeset detectedLatest commit: 2e9fa22 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
it looks like some typings in tests are failing after I updated the types to react18 (needed to be able to import
when trying to render |
Do u have any numbers for this optimization? Where the costs are in the old solution? This would be a breaking change so it will require a new major if we decide to land this |
why is it a breaking change? because To see the concrete differences in bundle size, I would need to gather a preview build of some sorts. But the uSES shim alone has about 3.11kb, and |
I'm fine with releasing this as a new major |
In general, the I made a patch that fixes the type issues (click to see)diff --git a/packages/xstate-react/test/createActorContext.test.tsx b/packages/xstate-react/test/createActorContext.test.tsx
index 27356bdc21..cf12ae8eb4 100644
--- a/packages/xstate-react/test/createActorContext.test.tsx
+++ b/packages/xstate-react/test/createActorContext.test.tsx
@@ -37,7 +37,7 @@ describe('createActorContext', () => {
const SomeContext = createActorContext(someMachine);
const Component = () => {
- const value = SomeContext.useSelector((state) => state.value);
+ const value = SomeContext.useSelector((state) => state.value as string);
return <div data-testid="value">{value}</div>;
};
@@ -76,7 +76,7 @@ describe('createActorContext', () => {
return (
<>
- <div data-testid="value">{state.value}</div>
+ <div data-testid="value">{state.value as string}</div>
<button
data-testid="next"
onClick={() => actorRef.send({ type: 'NEXT' })}
@@ -196,7 +196,7 @@ describe('createActorContext', () => {
const actor = SomeContext.useActorRef();
const value = useSelector(actor, (state) => state.value);
- return <div data-testid="value">{value}</div>;
+ return <div data-testid="value">{value as string}</div>;
};
const App = () => {
@@ -412,7 +412,7 @@ describe('createActorContext', () => {
actorRef.send({ type: 'next' });
}}
>
- {state.value}
+ {state.value as string}
</div>
);
};
diff --git a/packages/xstate-react/test/useActor.test.tsx b/packages/xstate-react/test/useActor.test.tsx
index eed3f6a0dd..ea8b20c30b 100644
--- a/packages/xstate-react/test/useActor.test.tsx
+++ b/packages/xstate-react/test/useActor.test.tsx
@@ -694,7 +694,7 @@ describeEachReactMode('useActor (%s)', ({ suiteKey, render }) => {
const [state, send] = useActor(machine);
return (
<>
- <div data-testid="result">{state.value}</div>
+ <div data-testid="result">{state.value as string}</div>
<button onClick={() => send({ type: 'EV' })} />
</>
);
@@ -740,7 +740,7 @@ describeEachReactMode('useActor (%s)', ({ suiteKey, render }) => {
);
return (
<>
- <div data-testid="result">{state.value}</div>
+ <div data-testid="result">{state.value as string}</div>
<button onClick={() => send({ type: 'EV' })} />
</>
);
diff --git a/packages/xstate-react/test/useActorRef.test.tsx b/packages/xstate-react/test/useActorRef.test.tsx
index 6739ec7230..a802e587b9 100644
--- a/packages/xstate-react/test/useActorRef.test.tsx
+++ b/packages/xstate-react/test/useActorRef.test.tsx
@@ -193,7 +193,7 @@ describeEachReactMode('useActorRef (%s)', ({ suiteKey, render }) => {
>
Send to child
</button>
- <div data-testid="child-state">{childState.value}</div>
+ <div data-testid="child-state">{childState.value as string}</div>
</>
);
};
@@ -251,7 +251,7 @@ describeEachReactMode('useActorRef (%s)', ({ suiteKey, render }) => {
>
Send to child
</button>
- <div data-testid="child-state">{childState.value}</div>
+ <div data-testid="child-state">{childState.value as string}</div>
</>
);
};
@@ -325,7 +325,7 @@ describeEachReactMode('useActorRef (%s)', ({ suiteKey, render }) => {
it('should work with a promise actor', async () => {
const promiseLogic = fromPromise(
- () => new Promise((resolve) => setTimeout(() => resolve(42), 10))
+ () => new Promise<number>((resolve) => setTimeout(() => resolve(42), 10))
);
const App = () => {
@@ -506,7 +506,7 @@ describeEachReactMode('useActorRef (%s)', ({ suiteKey, render }) => {
>
Send event
</button>
- <span>{value}</span>
+ <span>{value as string}</span>
</>
);
}
@@ -565,7 +565,7 @@ describeEachReactMode('useActorRef (%s)', ({ suiteKey, render }) => {
>
Send event
</button>
- <span>{value}</span>
+ <span>{value as string}</span>
</>
);
}
diff --git a/packages/xstate-react/test/useSelector.test.tsx b/packages/xstate-react/test/useSelector.test.tsx
index fc2c6552c3..3de48d3a97 100644
--- a/packages/xstate-react/test/useSelector.test.tsx
+++ b/packages/xstate-react/test/useSelector.test.tsx
@@ -682,7 +682,7 @@ describeEachReactMode('useSelector (%s)', ({ suiteKey, render }) => {
return (
<>
- <div data-testid="child-state">{childState.value}</div>
+ <div data-testid="child-state">{childState.value as string}</div>
<button
data-testid="child-send"
onClick={() => childRef.send({ type: 'NEXT' })} |
Thanks @davidkpiano, I've applied your patch |
# Conflicts: # packages/xstate-react/package.json # packages/xstate-store/package.json # yarn.lock
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.
cc. @Andarist for quick review
🎉 will you ship this as a major or minor? |
Likely a major since we changed how |