繁体   English   中英

使用redux将组件加载两次

[英]React component loading twice with redux

我正在尝试从redux存储中获取状态,但是每当我访问redux操作时,都会两次加载我的组件,通过第一次手动加载路由,它会返回两个对象,一个是空状态的对象,另一个是更新状态的对象,但是当我切换时在路由之间,它返回新状态,但每次返回两次,或者返回两个相同状态的对象。

这会导致在加载组件时发生不确定的错误,特别是在手动加载route时,当我访问任何状态属性时,

Redux action
export function getRelationshipStatus() {
    let headers = {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${localStorage.getItem('access_token')}`
    }
    return dispatch => {
        axios.get('http://localhost/relation/api/get-relationship-status', { headers })
        .then(  response => dispatch({type:ActionTypes.GET_RELATIONSHIP_STATUS,payload:response.data}))
    }
};

减速器

import React from 'react'
import {GET_RELATIONSHIP_STATUS} from  '../actions/action-types'
const initialState={
    profile:{},
}
export default function (state=initialState,action) {
    switch(action.type){
        case GET_RELATIONSHIP_STATUS:
        console.log(action.payload);
        return {...state,profile:action.payload}
        break;
        default:
        return state
    }
}

实际组成

import React, { Component } from 'react'
import {connect} from 'react-redux'
import {getRelationshipStatus} from '../../store/actions'
 class People extends Component {
     constructor(props){
         super(props)
     }
      componentDidMount(){
        this.props.getRelationshipStatus()
    }
    render() {
        console.log(this.props.profile)

        return (
            <div>


            </div>
        )
    }
}


function mapStateToProps(state) {
    return {
        profile: state.profile,
    }
}
export default connect(
    mapStateToProps,
    { getRelationshipStatus }
)(People);

嘿,这是react-redux加载的总自然行为的两倍。

甚至我都能向你解释,但这不是问题

我确定这种行为没有问题顺便问一下,错误名称是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM