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

Wrong counter action function in react-trrack-example #15

Open
thinkh opened this issue Aug 31, 2022 · 1 comment
Open

Wrong counter action function in react-trrack-example #15

thinkh opened this issue Aug 31, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@thinkh
Copy link
Member

thinkh commented Aug 31, 2022

Steps to reproduce

  1. Clone repo
  2. Run yarn dev:all
  3. Check console output

Observed behavior

[...rack-example] ERROR in ./apps/react-trrack-example/src/app/store/trrack.ts:46:7
[...rack-example] TS2345: Argument of type '(add: number) => { type: string; payload: number; meta: { hasSideEffects: boolean; }; }' is not assignable to parameter of type 'TrrackActionFunction<"increment-counter", string, any, number> | StateChangeFunction<number, number>'.
[...rack-example]   Type '(add: number) => { type: string; payload: number; meta: { hasSideEffects: boolean; }; }' is not assignable to type 'TrrackActionFunction<"increment-counter", string, any, number>'.
[...rack-example]     Property 'undo' is missing in type '{ type: string; payload: number; meta: { hasSideEffects: boolean; }; }' but required in type '{ do?: { payload: number; type: "increment-counter"; } | undefined; undo: { payload: any; type: string; }; }'.
[...rack-example]     44 |     const incrementCounter = reg.register(
[...rack-example]     45 |       'increment-counter',
[...rack-example]   > 46 |       (add: number) => {
[...rack-example]        |       ^^^^^^^^^^^^^^^^^^
[...rack-example]     47 |         setCounter((c) => c + add);
[...rack-example]     48 |         return {
[...rack-example]     49 |           type: 'decrement-counter',
[...rack-example] 
[...rack-example] ERROR in ./apps/react-trrack-example/src/app/store/trrack.ts:60:7
[...rack-example] TS2345: Argument of type '(sub: number) => { type: string; payload: number; meta: { hasSideEffects: boolean; }; }' is not assignable to parameter of type 'StateChangeFunction<number, number> | TrrackActionFunction<"decrement-counter", string, any, number>'.
[...rack-example]   Type '(sub: number) => { type: string; payload: number; meta: { hasSideEffects: boolean; }; }' is not assignable to type 'StateChangeFunction<number, number>'.
[...rack-example]     Type '{ type: string; payload: number; meta: { hasSideEffects: boolean; }; }' is not assignable to type 'number'.
[...rack-example]     58 |     const decrementCounter = reg.register(
[...rack-example]     59 |       'decrement-counter',
[...rack-example]   > 60 |       (sub: number) => {
[...rack-example]        |       ^^^^^^^^^^^^^^^^^^
[...rack-example]     61 |         setCounter((c) => c - sub);
[...rack-example]     62 |         return {
[...rack-example]     63 |           type: 'increment-counter',
[...rack-example] 
[...rack-example] Found 2 errors in 339 ms.

The action functions for incrementCounter and decrementCounter are probably not up-to-date with the most recent typings.

Expected behavior

The examples should compile without errors.

@thinkh
Copy link
Member Author

thinkh commented Aug 31, 2022

Changing the functions to the undo property resolves the compile errors:

    const incrementCounter = reg.register(
      'increment-counter',
      (add: number) => {
        setCounter((c) => c + add);
        return {
          undo: {
            type: 'decrement-counter',
            payload: add,
          },
        };
      }
    );

    const decrementCounter = reg.register(
      'decrement-counter',
      (sub: number) => {
        setCounter((c) => c - sub);
        return {
          undo: {
            type: 'increment-counter',
            payload: sub,
          },
        };
      }
    );

However, now I see runtime errors when I click the increment/decrement button.

TypeError
Cannot read properties of undefined (reading 'map')
Call Stack
 App
  main.js:78:61
 renderWithHooks
  vendor.js:37547:18
 updateFunctionComponent
  vendor.js:40825:20
 beginWork
  vendor.js:42838:16
 HTMLUnknownElement.callCallback
  vendor.js:25406:14
 Object.invokeGuardedCallbackDev
  vendor.js:25455:16
 invokeGuardedCallback
  vendor.js:25519:31
 beginWork$1
  vendor.js:48688:7
 performUnitOfWork
  vendor.js:47794:12
 workLoopSync
  vendor.js:47703:5

Initial state:

grafik

After clicking the counter button:

grafik

It looks like the tasks property is removed from the initial state with the counter methods. How does a valid counter action function look like?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant