我有一些react-redux项目。 我有一些组件,我不想列出api的一些数据。 只需在 componentDidMount 方法中从我的组件中调度操作即可获取数据。
刷新页面时,将console.log设置为redux状态。
组件渲染良好,但是当我查看控制台时,有console.log显示我的状态为空,然后正常显示我的状态。.在此过程中,我的组件给我一个错误,我的数据无法显示页面未定义。
所以这是因为我所理解的是fetch方法异步工作,但是当我的redux状态将从api获取数据然后呈现组件时,该怎么办?
这是我的操作:
export const FETCH_DATA_START = 'FETCH_DATA_START'
export const FETCH_DATA_SUCCESS = 'FETCH_DATA_SUCCESS'
export const FETCH_DATA_FAILED = 'FETCH_DATA_FAILED'
export const getData = () => {
return (dispatch) => {
dispatch({
type: FETCH_DATA_START
})
fetch("http://api.com", {
credentials: "include",
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then((res) => {
dispatch({
type: FETCH_DATA_SUCCESS,
payload: res
})
})
.catch((err) => {
console.log(err)
dispatch({
type: FETCH_DATA_FAILED,
payload: 'error'
})
})
}
}
这是我的减速器:
import { FETCH_DATA_START, FETCH_DATA_SUCCESS, FETCH_DATA_FAILED } from 'store/actions/getData'
let initialState = []
export default (state = initialState, action) => {
switch (action.type) {
case FETCH_DATA_START:
return [...state]
case FETCH_DATA_SUCCESS:
return [...state, ...action.payload]
case FETCH_DATA_FAILED:
return [state]
default:
return state
}
}
在我的组件中:
import React, {Component} from 'react'
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux'
import {getData} from 'store/actions/getData'
class Ops extends Component {
componentDidMount() {
this.props.getData()
}
render() {
console.log(this.props.dataOps)
return(
<div></div>
)
}
}
const mapStateToProps = state => ({dataOps: state.dataOps})
function mapDispatchToProps(dispatch) {
return {
getData: bindActionCreators(getData, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Ops)
答案 0 :(得分:0)
选项1
在使用前检查减速器状态
选项2
async componentDidMount(){
//set loader true
await this.props.getData;
//set loader false
}
答案 1 :(得分:0)
您可以实现“加载”状态
import React, {Component} from 'react'
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux'
import {getData} from 'store/actions/getData'
class Ops extends Component {
state = {
isLoading: true
}
componentDidMount() {
this.props.getData().then(res => res.json())
.then((res) => {
// Toggle state after promise completed
this.setState({
isLoading: false
})
// Dispatch data
dispatch({
type: FETCH_DATA_SUCCESS,
payload: res
})
})
.catch((err) => {
console.log(err)
dispatch({
type: FETCH_DATA_FAILED,
payload: 'error'
})
})
}
}
render() {
// If we're still loading data, show loading component
if (this.state.isLoading) {
return (<div>Loading...</div>);
}
console.log(this.props.dataOps)
// Render with data
return(
<div>Loading completed!</div>
)
}
}
const mapStateToProps = state => ({dataOps: state.dataOps})
function mapDispatchToProps(dispatch) {
return {
getData: bindActionCreators(getData, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Ops)
编辑;返回承诺而不是已解决的数据; (不确定语法)
export const getData = () => {
return (dispatch) => {
dispatch({
type: FETCH_DATA_START
});
return fetch("http://api.com", {
credentials: "include",
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
})
}