redux中如何通过api取数据?
How to fetch data through api in redux?
我是 reactjs/redux 的初学者,找不到一个简单易用的示例来说明如何使用 api 调用在 redux 应用程序中检索数据。我想您可以使用 jquery ajax 调用,但可能有更好的选择?
创建一个操作,您可以在其中执行对 API 的请求。您可以使用像 axios 这样的库或 fetch which return 一个承诺。
actions/index.js:
import axios from 'axios';
export const FETCH_SOMETHING= 'FETCH_SOMETHING;
const ROOT_URL = 'http://api.youapi.com';
export function fetchWeather(city) {
const url = `${ROOT_URL}&q=${aParamYouMayNeed}`;
const request = axios.get(url);
return {
type: FETCH_SOMETHING,
payload: request
};
}
然后在 reducer 中,使用如下解决后的 promise 结果:
reducers/reducer_something.js:
import { FETCH_SOMETHING} from '../actions/index';
export default function(state = [], action) {
switch (action.type) {
case FETCH_SOMETHING:
return [ action.payload.data, ...state ];
}
return state;
}
从 Stephen Grider 借来的代码。这是他的回购协议:https://github.com/StephenGrider/ReduxCasts/tree/master/weather/src
JSfiddle; http://jsfiddle.net/cdagli/b2uq8704/6/
它使用了 redux、redux-thunk 和 fetch。
获取方法;
function fetchPostsWithRedux() {
return (dispatch) => {
dispatch(fetchPostsRequest());
return fetchPosts().then(([response, json]) =>{
if(response.status === 200){
dispatch(fetchPostsSuccess(json))
}
else{
dispatch(fetchPostsError())
}
})
}
}
function fetchPosts() {
const URL = "https://jsonplaceholder.typicode.com/posts";
return fetch(URL, { method: 'GET'})
.then( response => Promise.all([response, response.json()]));
}
上面使用的操作:
(注意:您可以定义许多操作,例如 fetchPostRequest 可用于显示加载指示器。或者您可以针对不同的 HTTP 状态代码调度不同的操作。)
function fetchPostsRequest(){
return {
type: "FETCH_REQUEST"
}
}
function fetchPostsSuccess(payload) {
return {
type: "FETCH_SUCCESS",
payload
}
}
function fetchPostsError() {
return {
type: "FETCH_ERROR"
}
}
并且在您的减速器中,您可以将帖子加载到 state;
const reducer = (state = {}, action) => {
switch (action.type) {
case "FETCH_REQUEST":
return state;
case "FETCH_SUCCESS":
return {...state, posts: action.payload};
default:
return state;
}
}
连接后,您可以访问组件中的状态和操作;
connect(mapStateToProps, {fetchPostsWithRedux})(App);
我是 reactjs/redux 的初学者,找不到一个简单易用的示例来说明如何使用 api 调用在 redux 应用程序中检索数据。我想您可以使用 jquery ajax 调用,但可能有更好的选择?
创建一个操作,您可以在其中执行对 API 的请求。您可以使用像 axios 这样的库或 fetch which return 一个承诺。
actions/index.js:
import axios from 'axios';
export const FETCH_SOMETHING= 'FETCH_SOMETHING;
const ROOT_URL = 'http://api.youapi.com';
export function fetchWeather(city) {
const url = `${ROOT_URL}&q=${aParamYouMayNeed}`;
const request = axios.get(url);
return {
type: FETCH_SOMETHING,
payload: request
};
}
然后在 reducer 中,使用如下解决后的 promise 结果:
reducers/reducer_something.js:
import { FETCH_SOMETHING} from '../actions/index';
export default function(state = [], action) {
switch (action.type) {
case FETCH_SOMETHING:
return [ action.payload.data, ...state ];
}
return state;
}
从 Stephen Grider 借来的代码。这是他的回购协议:https://github.com/StephenGrider/ReduxCasts/tree/master/weather/src
JSfiddle; http://jsfiddle.net/cdagli/b2uq8704/6/
它使用了 redux、redux-thunk 和 fetch。
获取方法;
function fetchPostsWithRedux() {
return (dispatch) => {
dispatch(fetchPostsRequest());
return fetchPosts().then(([response, json]) =>{
if(response.status === 200){
dispatch(fetchPostsSuccess(json))
}
else{
dispatch(fetchPostsError())
}
})
}
}
function fetchPosts() {
const URL = "https://jsonplaceholder.typicode.com/posts";
return fetch(URL, { method: 'GET'})
.then( response => Promise.all([response, response.json()]));
}
上面使用的操作:
(注意:您可以定义许多操作,例如 fetchPostRequest 可用于显示加载指示器。或者您可以针对不同的 HTTP 状态代码调度不同的操作。)
function fetchPostsRequest(){
return {
type: "FETCH_REQUEST"
}
}
function fetchPostsSuccess(payload) {
return {
type: "FETCH_SUCCESS",
payload
}
}
function fetchPostsError() {
return {
type: "FETCH_ERROR"
}
}
并且在您的减速器中,您可以将帖子加载到 state;
const reducer = (state = {}, action) => {
switch (action.type) {
case "FETCH_REQUEST":
return state;
case "FETCH_SUCCESS":
return {...state, posts: action.payload};
default:
return state;
}
}
连接后,您可以访问组件中的状态和操作;
connect(mapStateToProps, {fetchPostsWithRedux})(App);