【问题标题】:Redux thunk fetch return undefinedRedux thunk fetch 返回 undefined
【发布时间】:2016-10-13 08:17:23
【问题描述】:

我是 Redux Thunk 的新手,在通过单击按钮组件获取 async 调用后,我遇到了 dispatch 操作的问题。

actions.js

import fetch from 'isomorphic-fetch'

export const getPosts = (json) => {
    return {
        type: constant.GET_POSTS,
        payload: {
            data: json
        }
    }
}

export const loadPosts () => {
    return (dispatch) => {
        return fetch('https://jsonplaceholder.typicode.com/posts')
            .then(res => {
                res.json()
            }).then(json => {
                dispatch(getPosts(json))
            })
    }
}

button.js

class Button extends React.Component {

    clicked(){
        console.log(this.props.loadJsonPosts()) // got undefined here
    }
    render() {
        return(
            <button onClick={this.clicked.bind(this)}>click</button>
        )
    }
}

buttonContainer.js

import connect from 'react-redux/lib/components/connect'
import { loadPosts } from '../actions/actions.js'
import Button from '../components/Button'

function mapDispatchToProps(dispatch) {
    return {
        loadJsonPosts: () => { dispatch(loadPosts()) }
    }
}

export default connect(null, mapDispatchToProps)(Button)

reducer.js

import * as constant from '../constants/index'

let initialState = { postList: [] }

const reducer = (state = initialState, action) => {

    switch (action.type) {
        case constant.GET_POSTS: //here i call my loadPosts action
            state = Object.assign({}, { postList: [{ post: action.data }] })
            break;
        default:
            break;
    }

    return state
}

export default reducer

App.jsx

import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import Main from './components/Main'
import thunk from 'redux-thunk'
import { createStore, applyMiddleware  } from 'redux'
import { Provider } from 'react-redux'
import reducer from './reducers/reducer'
const store = createStore(
    reducer,
    applyMiddleware(thunk)
)

class App extends Component {
    render() {
        return(
            <Provider store={store}>
                <Main />
            </Provider>
        )
    }
}

ReactDOM.render(
    <App />,
    document.getElementById('app')
)

我不知道为什么我得到undefined,也许我错过了什么或者我的方法有误

【问题讨论】:

  • 你在rootReducer中注册你的reducer了吗?

标签: javascript reactjs asynchronous redux thunk


【解决方案1】:

您忘记在 actions.js 中为下一个 then 块返回 res.json()。 应该是

export const loadPosts () => {
return (dispatch) => {
    return fetch('https://jsonplaceholder.typicode.com/posts')
        .then(res => {
            return res.json();
        }).then(json => {
            dispatch(getPosts(json))
        })
      }}

或者你可以通过写.then(res =&gt; res.json())删除块来跳过返回语句

【讨论】:

  • 你说得对 Vikramaditya,我已经改正了,但结果还是一样
  • mapDispatchToProps 函数中,您还应该返回类似loadJsonPosts: () =&gt; { return dispatch(loadPosts()) } 的函数,并且在reducer 中,您永远不应该为状态分配任何内容,而是应该简单地编写“case constant.GET_POSTS: return Object.分配({},{ postList:[{ post:action.payload.data }] }); `
  • 仍有一些我无法弄清楚的错误,但我建议再仔细阅读一下 redux 文档。你缺少一些 redux 的基础知识
【解决方案2】:

我遇到了同样的问题,发现在创建 redux 存储时确保 thunk 中间件在我的链中是第一个允许我访问我所追求的承诺,而不是获得undefined

store = createStore(
            rootReducer, 
            initialState,
            applyMiddleware(thunk, otherMiddleware1, otherMiddleware2)
        );

【讨论】:

  • 这对我来说也是正确的。即使在动作创建者中返回 true 也失败了。现在先移动它会返回预期值。
【解决方案3】:

mapDispatchToProps 应该是这样的:

function mapDispatchToProps(dispatch) {
return {
    // loadPosts instead of loadPosts()
    loadJsonPosts: () => { dispatch(loadPosts) }
} }

【讨论】:

    猜你喜欢
    • 2020-09-09
    • 2019-01-27
    • 2018-12-28
    • 2019-08-04
    • 2023-03-18
    • 2019-05-30
    • 2018-05-21
    • 2018-05-30
    • 2018-05-27
    相关资源
    最近更新 更多