DEV Community

Bronwyn
Bronwyn

Posted on

New redux learner. Help with my code

Hi team, what's wrong with this code? It's doesn't work. I'm new to redux, troubleshooting so far has not fixed it. It's only functionality is to add to an array with increase in number

`import { configureStore } from "@reduxjs/toolkit"
import todoReducer from "../features/todoSlice"

export default configureStore({
reducer: {
miniTodo: todoReducer
}
})`

`import { useSelector, useDispatch } from 'react-redux'
import { addToArray } from './features/todoSlice'

export function TodoList () {
const count = useSelector(state => state.miniTodo.value)
const dispatch = useDispatch()

return (
    <div>
        <button onClick={()=> dispatch(addToArray())}>Add Item</button>
    </div>
)
Enter fullscreen mode Exit fullscreen mode

}

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import reportWebVitals from './reportWebVitals';
import store from './app/store';
import { Provider } from 'react-redux';
import { TodoList } from './todo';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(



);`

`import { createSlice } from "@reduxjs/toolkit"

export const todoSlice = createSlice({
name: "miniTodo",
initialState: {
value: []
},
reducers: {
addToArray: state => {
const newThingText = Thing ${state.value.length + 1}
state.value.push(newThingText)
console.log(state.value)
},
useNotKnown: (state, action) => {
state.value += action.payload
}
}
})

export const { addToArray, useNotKnown} = todoSlice.actions

export default todoSlice.reducer
`

Top comments (0)