pixijs / pixi-react

Write PIXI apps using React declarative style
https://pixijs.io/pixi-react/
MIT License
2.24k stars 172 forks source link

Fix typing of oldProps in applyProps #473

Closed zOadT closed 3 months ago

zOadT commented 4 months ago
Description of change

Update the typings of applyProps to show that oldProps can be an empty object (this happens on the first render). To access properties on oldProps users can use the in operator to check against an empty object:

export default PixiComponent<
  { somePropName: string, somePropName2: string, },
  Container
>('Use', {
  create: () => {
    return new Container();
  },
  applyProps: (instance, oldProps, newProps) => {
    if (!('somePropName' in oldProps)) {
      // oldProps is empty
      // handle this case
      return;
    }
    // oldProps is typed as Readonly<{ somePropName: string; somePropName2: string; }>
    oldProps.somePropName;
    // ...
  },
});

Important: This is a breaking change! It only affects typings though, so users could very easily work around it by adding @ts-expect-error comments if they are affected and don't want to adapt their code. Also the change just reflects what is actually passed during runtime, so it might actually benefit the user if they are rechecking their code.

So I think it would be okay to introduce this change without a new major version, but I also totally understand if you want to include this only with the next major release.

Fixes: https://github.com/pixijs/pixi-react/issues/348

Pre-Merge Checklist
codesandbox-ci[bot] commented 4 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f187392fc7562c5cffb2e570c2347b4c29c378ef:

Sandbox Source
sandbox Configuration