Skip to content
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

[crud] Basic implementation #31416

Closed
wants to merge 1 commit into from
Closed

[crud] Basic implementation #31416

wants to merge 1 commit into from

Conversation

poteto
Copy link
Member

@poteto poteto commented Nov 4, 2024

This PR introduces a new experimental hook useResourceEffect, which is something that we're doing some very early initial tests on.

This may likely not pan out and will be removed or modified if so. Please do not rely on it as it will break.

Stack created with Sapling. Best reviewed with ReviewStack.

Copy link

vercel bot commented Nov 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 11, 2024 5:00pm

@react-sizebot
Copy link

react-sizebot commented Nov 5, 2024

The size diff is too large to display in a single comment. The GitHub action for this pull request contains an artifact called 'sizebot-message.md' with the full message.

Generated by 🚫 dangerJS against 0492b5f

packages/react-reconciler/src/ReactFiberCallUserSpace.js Outdated Show resolved Hide resolved
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
}
destroy = runWithFiberInDEV(finishedWork, callCreateInDEV, effect);
if (effect.kind === SimpleEffectKind) {
Copy link
Member

Choose a reason for hiding this comment

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

Ditto here for the feature flag behavior

Copy link
Member Author

Choose a reason for hiding this comment

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

this one is still needed because i unconditionally change the shape of the Effect type, so we do still need this check. i could probably console.error or something if the flag is disabled and the effect kind isn't the simple one

enableUseResourceEffectHook &&
effect.kind === ResourceEffectKind
) {
hookName = 'useResourceEffect';
Copy link
Member

Choose a reason for hiding this comment

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

Ditto no failing test

effect.resource != null &&
(effect.create != null ||
// TODO(@poteto) this feels gross
finishedWork.return == null)
Copy link
Member

Choose a reason for hiding this comment

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

This only works because in the tests the App component is the root component, so there's no return fiber to the parent. We need to find a better way to do this that works through multiple layers.

Copy link
Member

Choose a reason for hiding this comment

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

I.e. this test fails:

    it('unmounts on deletion after skipped effect', async () => {
      
      function Wrapper(props) {
        return <App {...props} />
      }
      function App({id, username}) {
        const opts = useMemo(() => {
          return {username};
        }, [username]);
        useResourceEffect(
          () => {
            const resource = new Resource(id, opts);
            Scheduler.log(`create(${resource.id}, ${resource.opts.username})`);
            return resource;
          },
          [id],
          resource => {
            resource.update(opts);
            Scheduler.log(`update(${resource.id}, ${resource.opts.username})`);
          },
          [opts],
          resource => {
            resource.destroy();
            Scheduler.log(`destroy(${resource.id}, ${resource.opts.username})`);
          },
        );
        return <Text text={'Id: ' + id} />;
      }

      await act(async () => {
        ReactNoop.render(<Wrapper id={0} username="Sathya" />, () =>
          Scheduler.log('Sync effect'),
        );
        await waitFor(['Id: 0', 'Sync effect']);
        expect(ReactNoop).toMatchRenderedOutput(<span prop="Id: 0" />);
      });

      assertLog(['create(0, Sathya)']);

      await act(async () => {
        ReactNoop.render(<Wrapper id={0} username="Lauren" />, () =>
          Scheduler.log('Sync effect'),
        );
        await waitFor(['Id: 0', 'Sync effect']);
        expect(ReactNoop).toMatchRenderedOutput(<span prop="Id: 0" />);
      });

      assertLog(['update(0, Lauren)']);

      ReactNoop.render(null);
      await waitForAll(['destroy(0, Lauren)']);
      expect(ReactNoop).toMatchRenderedOutput(null);
    });

) {
const destroy_ = resource == null ? destroy : destroy.bind(null, resource);
Copy link
Member

Choose a reason for hiding this comment

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

What if you return null as the resource?

Copy link
Member Author

Choose a reason for hiding this comment

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

This PR introduces a new experimental hook `useResourceEffect`, which is something that we're doing some very early initial tests on.

This may likely not pan out and will be removed or modified if so. Please do not rely on it as it will break.
@poteto
Copy link
Member Author

poteto commented Nov 12, 2024

Sapling messed up my PR, opening a new one

@poteto poteto deleted the pr31416 branch November 12, 2024 21:02
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.

4 participants