gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:13:52 25 4
gpt4 key购买 nike

我想从 GitHub API 获得包含存储库的响应,但我得到的只是一个空数组,这是我第一次在 React 中使用 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
});

Action / Action .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
});
}
}
);
}
}

页面.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 组件并在那里进行操作!请让我知道我应该改变什么以及我做错了什么,感谢您的帮助!!

最佳答案

从服务器返回的 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,
});
}
});
};
}

关于javascript - 我无法使用带有 react.js 的 redux 从 github api 获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57331495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com