【问题标题】:redux function mapDispatchToProps error in calling an APIredux 函数 mapDispatchToProps 调用 API 时出错
【发布时间】:2019-12-11 07:33:26
【问题描述】:

我想在 reactjs 中使用 redux 从 GitHub 的 API 获取存储库列表,但出现此错误:

./src/components/layouts/page/index.js 第 14 行:解析错误: 意外的令牌

function mapDispatchToProps(dispatch) {
         ^
    return {
         fetchRepos: function() {
         dispatch(fetchRepos());

这些是我的文件:

actions.js

export function fetchRepos() {
  return function(dispatch) {
    dispatch({
      type: 'FETCH_REPOS_REQUEST'
    });

    return fetch('curl https://api.github.com/search/repositories?q=sort=stars&order=desc')
      .then(response => response.json().then(body => ({ response, body })))
      .then(({ response, body }) => {
        if (!response.ok) {
          dispatch({
            type: 'FETCH_REPOS_FAILURE',
            error: body.error
          });
        } else {
          dispatch({
            type: 'FETCH_REPOS_SUCCESS',
            repos: body.repos
          });
        }
      }
    );
  }
}

app.js

import React from 'react';
import './App.css';
import Page from './components/layouts/page/index';
import { Provider } from 'react-redux'

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/index';

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

function App() {
  return (
    <Provider store={store}>
      <Page />
    </Provider>
  );
}

export default App;

reducers/index.js

import { combineReducers } from 'redux';

import repos from './reducers';

const rootReducer = combineReducers({
    repos,
});

export default rootReducer;

reducers/reducers.js

import { combineReducers } from 'redux';

const INITIAL_STATE = {
  items: [],
  isFetching: false,
  error: undefined
};

function reposReducer(state = INITIAL_STATE, action) {
  switch (action.type) {
    case 'FETCH_REPOS_REQUEST':
      return Object.assign({}, state, {
        isFetching: true
      });
    case 'FETCH_REPOS_SUCCESS':
      return Object.assign({}, state, {
        isFetching: false,
        repos: action.repos
      });
    case 'FETCH_REPOS_FAILURE':
      return Object.assign({}, state, {
        isFetching: false,
        error: action.error
      });
    default:
      return state;
  }
}

export default combineReducers({
  repos: reposReducer
});

Page.js

import React, { Component } from 'react';
import List from '../list/index.js';    
import { connect } from 'react-redux';
import { fetchRepos } from '../../../actions/actions';
import './page.scss';

class Page extends Component {
  componentDidMount() {
    this.props.fetchRepos();
  }

  function mapDispatchToProps(dispatch) {
    return {
      fetchRepos: function() {
        dispatch(fetchRepos());
      }
    };
  }

  function mapStateToProps(state) {
    return {
      repos: state.repos
    };
  }

  render() {
    return <List items={this.props.repos}/>
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Page);

【问题讨论】:

    标签: javascript reactjs redux react-redux


    【解决方案1】:

    您的mapDispatchToPropsmapStateToProps 需要在您的组件之外定义,以便connect 使用:

    class Page extends Component {
      componentDidMount() {
        this.props.fetchRepos();
      }
    
      render() {
        const { repos } = this.props;
    
        return <List items={repos} />;
      }
    }
    
    const mapDispatchToProps = dispatch => ({
      fetchRepos: () => dispatch(fetchRepos())
    });
    
    const mapStateToProps = state => ({
      repos: state.repos
    });
    
    export default connect(
      mapStateToProps,
      mapDispatchToProps
    )(Page);
    

    更多信息请参考this

    【讨论】:

    • 嘿,错误消失了,但我得到空响应对象
    • 再看一遍,您可能应该在您的fetchRepos 方法中去掉fetch() 调用中的'curl'。
    • 我做了,但仍然没有回应,StackOverflow 说我不能再问问题了
    • 也许这只是一个冷却时间,您必须稍等片刻,或者您已达到问题限制,需要提供积极的收到答案以“解锁”提出更多问题的能力。也请接受这个答案,因为它解决了您最初的问题。 How to accept an answer
    猜你喜欢
    • 2018-10-22
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    相关资源
    最近更新 更多