Skip to content

PayloadAction with meta does not work #376

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

Closed
SpaghettiC0des opened this issue Feb 20, 2020 · 5 comments
Closed

PayloadAction with meta does not work #376

SpaghettiC0des opened this issue Feb 20, 2020 · 5 comments

Comments

@SpaghettiC0des
Copy link

v1.3.0-alpha.8

Please check my example here

@phryneas
Copy link
Member

phryneas commented Feb 20, 2020

The default behaviour of RTK is to create an action creator with only one argument and only a payload property. This behaviour cannot be modified by TS annotations, because TS annotations have no effect on runtime behaviour.

You can use the prepare notation to define a function that overrides this default behaviour.
This function has the same signature & behaviour as the prepare argument for createAction, so you can find most of the relevant documentation there.

In your specific case, you'd want something along the lines of

const cakeSlice = createSlice({
  name: 'cake',
  initialState,
  reducers: {
    fetchAll: {
      reducer(
        state,
        action: PayloadAction<Cake[], string, { currentPage: number }>
      ) {
        state.all = action.payload
        state.meta = action.meta
      },
      prepare(payload: Cake[], currentPage: number) {
        return { payload, meta: { currentPage } }
      }
    }
  }
})

PRs to make this more clear in the docs are always welcome!

@SpaghettiC0des
Copy link
Author

My tsconfig

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "target": "esnext",
    "module": "commonjs",
    "lib": [
      "es6"
    ],
    "allowJs": true,
    "jsx": "react-native",
    "noEmit": true,
    "incremental": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "baseUrl": "./src",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true

  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ]
}

@phryneas
Copy link
Member

Unfortunately, there's nothing wrong with that as well. Could you try to reproduce the problem in a codesandbox? Without something we can both access, it's just guesswork :/

@SpaghettiC0des
Copy link
Author

SpaghettiC0des commented Feb 22, 2020

@phryneas #350 and this comment fixed the problem. Thanks for your time!

@phryneas
Copy link
Member

Please make sure to report accurate version information in the future.
I could have told you that from the beginning, but #350 was already a part of v1.3.0-alpha.8...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants