Answer the question
In order to leave comments, you need to log in
Redux-thunks, why is the thunk not working?
Good afternoon. For a long time I decided to switch from class components to functionality, and now, the time has come. Plus, I decided to use the Redux toolkit (well, what, go like that with a twinkle), now there are many questions.
One of them: for some obscure magical reason, the sled does not work at all.
Here is my side:
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit';
import { todoReducer } from './todo-reducer';
export const store = configureStore({
reducer: {
todoReducer
},
middleware: getDefaultMiddleware(),
devTools: process.env.NODE_ENV !== 'production',
});
import { createAction, createReducer } from "@reduxjs/toolkit"
import { todoAPI } from './api'
const addTodo = createAction('ADD_TODO')
const newTodoText = createAction('NEW_TODO_TEXT')
const toggleFetching = createAction('TOGGLE_FETCHING')
const setTasks = createAction('SET_TASKS')
let initialState = {
taskCount: 1,
newTodoText: '',
isFetching: false,
tasks: [],
};
export const todoReducer = createReducer(initialState, {
[addTodo]: (state) => {
state.taskCount++
},
[newTodoText]: (state, action) => {
state.newTodoText = action.payload
},
[toggleFetching]: (state) => {
state.isFetching = !state.isFetching
},
[setTasks]: (state, action) => {
state.tasks = action.payload
}
});
// thunk
export const getTodos = () => dispatch => {
dispatch(toggleFetching())
todoAPI.getTasks()
.then(response => {
dispatch(setTasks(response))
dispatch(toggleFetching())
})
}
import React from 'react'
import { useEffect } from 'react'
import { getTodos } from '../redux/todo-reducer'
export const Todo = (props) => {
useEffect(() => {
getTodos()
})
return (
<div>
Todo page
</div>
)
}
Answer the question
In order to leave comments, you need to log in
The call to getTodos() in the effect must be wrapped in dispatch.
import React from 'react'
import { useEffect } from 'react'
import { useDispatch } from 'react-redux'
import { getTodos } from '../redux/todo-reducer'
export const Todo = (props) => {
const dispatch = useDispatch()
useEffect(() => {
dispatch(getTodos())
})
...
}
Hello! I downloaded the file from the Internet, but I didn't need it. How can I download it back now? (with)
Users upload and download books or what? Basically, there is a two-table structure Books and Users with binding Users.id = Books.user_id
If the user deletes the book, then this is his last downloaded book, then we delete his record.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question