【问题标题】:Redux Thunk for simple async requestsRedux Thunk 用于简单的异步请求
【发布时间】:2019-01-26 19:32:03
【问题描述】:

我正在构建一个简单的应用程序来从 API URL 检索一些食谱。 我正在阅读 Thunk 的文档来实现它,但我不明白如何设置异步获取请求。 奇怪的是,如果我 console.log 传递给减速器的动作,它肯定会检索到正确的对象(鸡丝食谱列表)。 相反,当我将操作传递给减速器时,它会引发错误:

“未处理的拒绝(错误):给定操作“FETCH_RECIPES”,reducer“recipes”返回未定义。要忽略操作,您必须显式返回先前的状态。如果您希望此 reducer 不保存任何值,则可以返回 null而不是未定义。”

我的动作创建器有什么错误吗? API 调用是否正确完成?

商店

import React from 'react';
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createStore, applyMiddleware } from 'redux'
import SearchBar from './components/App';
import thunk from 'redux-thunk';
import 'bootstrap/dist/css/bootstrap.css';



import rootReducer from './reducers';

const store = createStore(rootReducer, applyMiddleware(thunk));

render(
    <Provider store={store}>
      <SearchBar />
    </Provider>,
    document.getElementById('root')
  )

组件

import React, { Component } from 'react';
import { connect } from 'react-redux';
import './App.css';
import { Button } from 'reactstrap';
import { Form } from 'reactstrap';
import { bindActionCreators } from 'redux';
import  {fetchRecipe } from '../actions';

class SearchBar extends Component {
  constructor(props) {
    super(props)


    this.state = { term: ''};
    this.typeRecipe = this.typeRecipe.bind(this)
    this.onFormSubmit = this.onFormSubmit.bind(this);  
  }

  onFormSubmit(e) {
    e.preventDefault()

    this.props.fetchRecipe(this.state.term)
  }


  typeRecipe(e) {
    this.setState({term: e.target.value});

  }

  render() {
    return (
      <div className="SearchBar">
         <Form onSubmit={this.onFormSubmit}>
            <input type='text'
            value={this.state.term}
            placeholder='ciao'
            onChange={this.typeRecipe}
             />
             <br/>
            <Button id='ciao' className='btn-success'>Submit</Button>
         </Form>
      </div>
    );
  }
}


function mapDispatchToProps(dispatch) {
  return bindActionCreators({ fetchRecipe }, dispatch);
}


export default connect(null, mapDispatchToProps)(SearchBar);

动作创建者

import axios from 'axios';

export const FETCH_RECIPES = 'FETCH_RECIPES';


const API_KEY = 'xxx';//not the real one.

export function fetchRecipe() {
   const request = axios.get(`http://food2fork.com/api/search?key=${API_KEY}&q=shredded%20chicken`);

   return (dispatch) => {
    request.then(({data}) =>{
        dispatch({ type: FETCH_RECIPES, payload: data})
    })
}


}

减速器

import { FETCH_RECIPES } from '../actions';

export default function (state = {}, action) {
    switch(action.type) {
        case FETCH_RECIPES:
            const newState = action.payload.data;                    
            return newState;
        default:
            return state
 }
}

combineReducer(索引)

import recipeReducer from '../reducers/recipes_reducer';
import { combineReducers } from 'redux';

const rootReducer = combineReducers({
    recipes: recipeReducer 
});

export default rootReducer;

【问题讨论】:

  • 在reducer中尝试newState = action.payload而不是newState = action.payload.data;

标签: javascript api asynchronous redux redux-thunk


【解决方案1】:

reducer return 语句有错误。

export default function (state = {}, action) {
    switch(action.type) {
        case FETCH_RECIPES:
            const newState = {...state, data : action.payload.data};                    
            return newState;
        default:
            return state
 }
}

这里我们将data 键添加到reducer 状态,要访问它,您可以在容器中使用:

export default connect((state)=>{
    var mapStateToProps = {};
    if(state.recipes.data) {
        mapStateToProps['recipes'] =  state.recipes.data
    }
    return mapStateToProps;    

 }, mapDispatchToProps)(SearchBar);

配方数据将以this.props.recipes 的形式提供。

【讨论】:

  • 抛出此错误:“Connect(SearchBar) 中的 mapStateToProps() 必须返回一个普通对象。而是收到未定义的。”我认为容器仍然没有收到数据
  • @Enrico 我已经根据您的错误更新了代码,检查问题是否仍然存在
  • 嗯,还是什么都没有。当我使用 {this.props.recipes} 渲染它们时,仍然会引发该错误。在网络选项卡中一切都很好,所以我认为减速器或组件有问题
  • 如果你通过null而不是mapStateToProps,你仍然得到同样的错误吗?
  • 是的...渲染方法无法访问属性。如果你想更好看,我正在 github 上上传一个 repo
猜你喜欢
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 2021-12-11
  • 2017-11-01
  • 1970-01-01
  • 2018-01-31
  • 2020-10-28
  • 2017-10-23
相关资源
最近更新 更多