J
J
jj swift2020-08-11 12:46:37
React
jj swift, 2020-08-11 12:46:37

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',
});


Here is my reducer:
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())
        })
}


Well, the actual component:

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>
    )
}


What am I doing wrong, why is the sled not working? The request is simply not made. I put the console log in the sledge, even it is not called. Just nothing happens, the rest of the application works.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2020-08-11
@jfswift

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())
    })

    ...
}

F
FanatPHP, 2015-03-20
@FanatPHP

Hello! I downloaded the file from the Internet, but I didn't need it. How can I download it back now? (with)

The task is kind of crazy, especially in terms of
And the structure is primitive:
a table of books
a table of users
a table of downloads of the form book_id-user_id

M
Maxim Uglov, 2015-03-21
@Vencendor

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 question

Ask a Question

731 491 924 answers to any question