我无法使用带有react.js的redux从github api获得响应

时间:2019-08-02 18:04:07

标签: javascript reactjs github redux react-redux

我想从GitHub API获得包含存储库的响应,我得到的只是一个空数组,这是我第一次使用redux和redux thunk进行反应,这是我的代码:

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

actions / actions.js

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

    return fetch('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
          });
        }
      }
    );
  }
}

pages.js

import React, { Component } from 'react';
import List from '../list/index.js';
import './page.scss';

import { connect } from 'react-redux';
import { fetchRepos } from '../../../actions/actions';

class Page extends Component {

  componentDidMount() {
    this.props.fetchRepos();
  }

  render() {
    console.log(this.props);
    return <div className="page"><List items={this.props.repos}/></div>
  }
}

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

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

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

我想以数组的形式获取响应,并将其作为prop传递给List组件并在那里进行操作!请让我知道我应该更改什么以及我做错了什么,谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

从服务器返回的json具有body.items,您正尝试从body.repos读取它。

将您的操作更改为:

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

    return fetch(
      "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.items,
          });
        }
      });
  };
}