Search code examples
reactjsreact-nativereduxreact-reduxredux-toolkit

(react redux toolkit) i want to know using action with multiple prameters


i recently started @reduxjs / toolkit. when i see code or documents of this, it not is not worked (undefined...) or too complicated(prepare?? createaciton??). i think using multiple parameters with reducer and action is simple when use redux toolkit, but it is clear that i am misunderstood. do you have any opinion or usage of that? this is reducer.

import {createSlice} from '@reduxjs/toolkit';

const initialState = {
    posts : [],
}


export const postSlice = createSlice({
    name: 'postReducer',
    initialState,
    reducers: {
        addPost: (state,action) => {
            state.posts = [{
                idx: action.idx,
                userid: action.userid,
                title: action.title,
                category: action.category,
                },
                ...state.posts,
            ]}
    },
  })
  
export const { addPost } = postSlice.actions;

export default postSlice.reducer;

store.js

import {configureStore} from '@reduxjs/toolkit';
import postReducer from '../reducers/PostReducer';

export const store = configureStore({
    reducer: {postReducer: postReducer},
  })

when i use it,

import { useDispatch,useSelector } from 'react-redux'
import { addPost } from '../reducers/PostReducer';
const dispatch = useDispatch()
onPress = {dispatch(addPost(?????));}

redux is too hard... p.s i use react-native.


Solution

    1. revise action.anything -> action.payload.anything
    2. dispatch(addPost({param:value,param:value....})
      it is simple with redux toolkit.