简体   繁体   English

Redux typescript 出口 state 减速机阵列

[英]Redux typescript exporting state of reducer in array

So I'm having a problem wherein my reducer is undefined and I don't freaking understand what it means.所以我遇到了一个问题,我的减速器是未定义的,我不明白这意味着什么。 It's working when it is not array but when array it is not working.当它不是数组时它正在工作,但是当它不工作时它是数组。

My code should be like this.我的代码应该是这样的。 In my groupSlice.ts it is something like this在我的groupSlice.ts中是这样的

export interface GroupState {
    grouplist: (string)[];
}

const groupInitialState: GroupState = {
    grouplist:['Binary','Jhonny Sins']
}
export const createGroup = createSlice({
    name: 'group',
    initialState:groupInitialState,
    reducers:{  
        addGroup: (state,action) => {
            state.grouplist.push(action.payload)
        },
        subGroup: ( state,action ) => {
            state.grouplist.filter((group) => group != action.payload)
        }
    }
})
...

And then I store it here at my store.ts然后我将它存储在我的store.ts

import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
import { createGroup }  from '../features/counter/groupSlice';

export const store = configureStore({
  reducer: {
    groupings: createGroup.reducer,
  },
});

export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export type AppThunk<ReturnType = void> = ThunkAction<
  ReturnType,
  RootState,
  unknown,
  Action<string>
>;

And then the hook.ts to pass on my state in reducer.然后hook.ts在减速器中传递我的state

import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import type { RootState, AppDispatch } from './store';

export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

but when I pass it here in my Group.tsx it says my grouplist is undefined even I already input a an array at string[]但是当我在我的Group.tsx中传递它时,它说我的grouplist是未定义的,即使我已经an array at string[]

import React,{ useState,useEffect } from 'react'

import styles from './Group.module.css'

import {
    addGroup,
    subGroup
} from './groupSlice';
import { useAppSelector, useAppDispatch } from '../../app/hooks';
import { RootState } from '../../app/store';

// HERE IS MY ERROR THAT DOESN"T WORK>
export const selectCount = (state: RootState) =>  {
    console.log(state.groupings?.grouplist)
    const groupList = state.groupings?.grouplist
    return groupList;
}

const Grouping = () => {

    const groups = useAppSelector(selectCount);
    const dispatch = useAppDispatch();
    const [groupName, setGroupName] = useState('');
      
    return (
        ...
    )
}

export default Grouping

So I'm trying to say that my grouplist is undefined but I don't know why since I already input a list values there.所以我grouplist我的组列表是未定义的,但我不知道为什么,因为我已经在那里输入了一个列表值。 Can anyone spotted the mistake here?任何人都可以在这里发现错误吗? Thanks.谢谢。

In the subGroup reducer:subGroup减速器中:

state.grouplist.filter(...)

This only returns the filtered array, it doesn't change it in place (unlike push ), so you need to reassign it:这只返回过滤后的数组,它不会改变它(不像push ),所以你需要重新分配它:

state.grouplist = state.grouplist.filter(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM