Redux 从 api 获取数据
Redux fetch data from api
我正在尝试使用 Redux 从 api 中获取一些数据。我的代码如下所示:
操作:
// Import libraries
import axios from 'axios';
// Import types
import {
GET_ALL_PICKS
} from './types';
export const getAllPicks = ({ token }) => {
const getPicks = (dispatch) => {
axios({
method: 'get',
url: 'http://myapi/',
headers: {
Authorization: `Bearer ${token}`
}
})
.then((response) => {
console.log(response.data); // First log here returns data just fine
dispatch({
type: GET_ALL_PICKS,
payload: response.data
});
})
.catch((error) => {
console.log(error);
});
};
return getPicks;
};
减速器:
// Import types
import {
GET_ALL_PICKS
} from '../actions/types';
// Set Initial State
const INITIAL_STATE = {
allPicks: {},
loading: false,
error: ''
};
// Make pick reducers
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case GET_ALL_PICKS:
return { ...state, allPicks: action.payload }; // Logging action.payload here returns data just fine
default:
return state;
}
};
组件:
// Import Libraries
import React, { Component } from 'react';
import { Text } from 'react-native';
import { connect } from 'react-redux';
import {
getAllPicks
} from '../actions/picks';
// Make Component
class HomeScreen extends Component {
// Fetch Data
componentWillMount() {
const { token } = this.props;
this.props.getAllPicks({ token });
}
// Test response
componentDidMount() {
console.log(this.props.allPicks); // This log returns empty object, why?!
}
render() {
return (
<Text>Test</Text>
);
}
}
const mapStateToProps = ({ auth, picks }) => {
const { token } = auth;
const { allPicks } = picks;
return {
token,
allPicks
};
};
export default connect(mapStateToProps, { getAllPicks })(HomeScreen);
当我 运行 应用程序时,我在操作 console.log
中看到数据,如果我在减速器中 运行 a console.log(action.payload)
我看到数据很好但是在组件中,我看到一个空数组,这表明我没有正确连接 reducer 中的数据?这是日志的屏幕截图:
谷歌搜索后,我也在我的减速器中尝试过这个:
return Object.assign({}, state, {
allPicks: action.payload
});
但我又得到了相同的结果。任何人都可以向我解释我做错了什么吗?
您混淆了组件生命周期和 API 生命周期。
实际情况是:
- componentWillMount
- getAllPicks
- componentDidMount(此时API没有return,picks为空)
- [...等待 API 到 return]
- 然后 API return 的数据,但为时已晚
然后你需要做的是在 render()
函数中检查你的 "picks" 状态,每次你的状态改变时都会更新(当 API returns),多亏了 connect()
函数。
您还可以使用 componentWillUpdate
检查选择是否正确更新,而不是 componentDidMount
,这又与正在更新的道具无关。
我正在尝试使用 Redux 从 api 中获取一些数据。我的代码如下所示:
操作:
// Import libraries
import axios from 'axios';
// Import types
import {
GET_ALL_PICKS
} from './types';
export const getAllPicks = ({ token }) => {
const getPicks = (dispatch) => {
axios({
method: 'get',
url: 'http://myapi/',
headers: {
Authorization: `Bearer ${token}`
}
})
.then((response) => {
console.log(response.data); // First log here returns data just fine
dispatch({
type: GET_ALL_PICKS,
payload: response.data
});
})
.catch((error) => {
console.log(error);
});
};
return getPicks;
};
减速器:
// Import types
import {
GET_ALL_PICKS
} from '../actions/types';
// Set Initial State
const INITIAL_STATE = {
allPicks: {},
loading: false,
error: ''
};
// Make pick reducers
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case GET_ALL_PICKS:
return { ...state, allPicks: action.payload }; // Logging action.payload here returns data just fine
default:
return state;
}
};
组件:
// Import Libraries
import React, { Component } from 'react';
import { Text } from 'react-native';
import { connect } from 'react-redux';
import {
getAllPicks
} from '../actions/picks';
// Make Component
class HomeScreen extends Component {
// Fetch Data
componentWillMount() {
const { token } = this.props;
this.props.getAllPicks({ token });
}
// Test response
componentDidMount() {
console.log(this.props.allPicks); // This log returns empty object, why?!
}
render() {
return (
<Text>Test</Text>
);
}
}
const mapStateToProps = ({ auth, picks }) => {
const { token } = auth;
const { allPicks } = picks;
return {
token,
allPicks
};
};
export default connect(mapStateToProps, { getAllPicks })(HomeScreen);
当我 运行 应用程序时,我在操作 console.log
中看到数据,如果我在减速器中 运行 a console.log(action.payload)
我看到数据很好但是在组件中,我看到一个空数组,这表明我没有正确连接 reducer 中的数据?这是日志的屏幕截图:
谷歌搜索后,我也在我的减速器中尝试过这个:
return Object.assign({}, state, {
allPicks: action.payload
});
但我又得到了相同的结果。任何人都可以向我解释我做错了什么吗?
您混淆了组件生命周期和 API 生命周期。
实际情况是:
- componentWillMount
- getAllPicks
- componentDidMount(此时API没有return,picks为空)
- [...等待 API 到 return]
- 然后 API return 的数据,但为时已晚
然后你需要做的是在 render()
函数中检查你的 "picks" 状态,每次你的状态改变时都会更新(当 API returns),多亏了 connect()
函数。
您还可以使用 componentWillUpdate
检查选择是否正确更新,而不是 componentDidMount
,这又与正在更新的道具无关。