Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Sate does not change on dispatch React Native - Redux

$
0
0

I´m trying to fix this bug since one day and it won´t work..I´m using dispatch but it never changes the state.The Slice should be right (it is really near to the online example from the redux-toolkit homepage )

This is my code:

App.tsx

export const StateDisplay = () => {  const data = useAppSelector(selectErrors);  const dispatch = useAppDispatch();  function onPressMethod() {    dispatch(      errorActions.addNewOne({ type: "thisIsAType", value: "thisIsAValue" })    );  }  return (<View><Text>STATE :</Text><Button title="add" onPress={onPressMethod}></Button><FlatList        data={data}        renderItem={({ item }) => <Text>{item.value}</Text>}      /><Text></Text></View>  );};

Store:

export const store = configureStore({    reducer: {        errors: errorListSlice.reducer,    },});

Slice:

export interface dto {    value: string;    type: string;}export type RootState = ReturnType<typeof store.getState>;export type AppDispatch = typeof store.dispatch;export const selectErrors = (state: RootState) => state.errors;const initialState: dto[] = [    { value: "val1", type: "type1" },    { value: "val1", type: "type1" },];export const errorListSlice = createSlice({    name: "errorList",    initialState,    reducers: {        addNewOne: (state, action: PayloadAction<dto>) => {            state = [                ...state,                {                    value: action.payload.value,                    type: action.payload.type,                },            ];        },    },});export const errorActions = errorListSlice.actions;export default errorListSlice.reducer;

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>