【问题标题】:Redux action needing to be called twice before updating state?在更新状态之前需要调用两次 Redux 操作?
【发布时间】:2020-04-10 08:06:35
【问题描述】:

我正在调用一个 redux 操作,它向数据库发出 get 请求并返回一个包含正确数据的对象。我已经通过系统跟踪对象,显示它一直正确发送到反应组件,但是,当调用 redux 操作时,对象没有得到更新。如果进行第二次调用,则更新状态。我认为这与异步行为有关,但我不确定如何修复它。

以下是调用该操作的组件的相关部分:

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { MDBContainer, MDBInput, MDBBtn } from 'mdbreact';
import { getSinglePatient } from '../../../actions/patientActions';
import PatientConfirmModal from '../../modals/PatientConfirmModal';

export class SelectPatient extends Component {
    state = {
        patientConfirmModalToggle: false,
        patient: {},
        searchID: ''
    };

    static propTypes = {
        patient: PropTypes.object.isRequired,
        getSinglePatient: PropTypes.func.isRequired
    };

    onChange = e => {
        this.setState({ [e.target.name]: e.target.value });
    };

    toggleConfirmModal = () => {
        this.setState({
            patientConfirmModalToggle: !this.state.patientConfirmModalToggle
        });
    };


    searchForPatient = () => {
        this.props.dispatch(getSinglePatient(this.state.searchID)); // edit 1
        console.log(this.props.patient);
        const newPatient = this.props.patient.patients[0];

        console.log(newPatient);
        if (newPatient !== undefined) {
            this.setState({
                patient: newPatient
            });
            this.toggleConfirmModal();
        }
        console.log(this.state.patient);
    };

    render() {
        return (
            <MDBContainer>
                <h3>Please enter a Patient ID to begin an assessment</h3>
                <MDBInput
                    label="Patient ID"
                    group
                    type="text"
                    name="searchID"
                    id="searchID"
                    onChange={this.onChange}
                />
                <MDBBtn
                    onClick={() => this.searchForPatient()}
                >
                    Search
                </MDBBtn>
                <PatientConfirmModal
                    modal={this.state.patientConfirmModalToggle}
                    patient={this.state.patient}
                    toggle={this.toggleConfirmModal}
                />
            </MDBContainer>
        );
    }
}

function mapStateToProps(state){
    console.log(state)
    return{
        patient: state.patient
    }
}

export default  connect(mapStateToProps , {getSinglePatient})(SelectPatient);

这是动作本身:

export const getSinglePatient = id => (dispatch, getState) => {
    dispatch(setPatientsLoading());
    axios
        .get(`/api/patients/${id}`, tokenConfig(getState))
        .then(res =>
            dispatch({
                type: GET_SINGLE_PATIENT,
                payload: res.data[0]
            })
        )
        .catch(err =>
            dispatch(returnErrors(err.response.data, err.response.status))
        );
};

这是发送到的路线:

// @route   GET api/patients
// @desc    Get a single patient
// @access  Public
router.get('/:id', (req, res) => {
    console.log('GET single request hit (ID:' + req.params.id + ')');
    Patient.find({ patientID: req.params.id }).then(patient => {
        res.json(patient);
    });
});

以下是控制台日志的结果(第一个结果是按一次搜索按钮,第二个是再次按并再次调用该操作):

编辑:来自下面建议的代码的更新错误(患者已切换到客户端,但这对代码没有影响,因为它已被完全重构)

结果:

【问题讨论】:

  • 也许使用异步 await searchForPatient = asnyc () => { await this.props.dispatch(getSinglePatient(this.state.searchID)); ......`你的商店在哪里?
  • @Omer 在没有 .dispatch 的情况下添加它仍然需要运行两次,而 .dispatch 仍然在屏幕截图结果中给出错误

标签: javascript reactjs redux mdbreact


【解决方案1】:
    //make sure you are dispatching your action like below
   // this.props.dispatch(your_action())
     
     searchForPatient = () => {
        this.props.dispatch(getSinglePatient(this.state.searchID)); //make change
        console.log(this.props.patient);
        const newPatient = this.props.patient.patients[0];

        console.log(newPatient);
        if (newPatient !== undefined) {
            this.setState({
                patient: newPatient
            });
            this.toggleConfirmModal();
        }
        console.log(this.state.patient);
    }
   
     
    //use mapStateToProps function  to access your resultent data comes from server : you can get your result in this.props
   //put this funcion above export default co.... 
   function mapStateToProps(state){
        console.log(state)
        return{
            patient : state.patient
        }
    }


    export default  connect(mapStateToProps , {getSinglePatient} )(SelectPatient)

【讨论】:

  • 使用这些更改运行会给出 _this.props.dispatch 不是函数的错误。当它得到一个承诺返回时,该操作实际上正在被调度:axios .get(/api/patients/${id}, tokenConfig(getState)) .then(res =&gt; dispatch({ type: GET_SINGLE_PATIENT, payload: res.data[0] }) ) 但需要在实际更新任何状态之前调用两次?
  • 请在您的问题中编辑您更新的 SelectedPatient 类。所以,我可以纠正你。
  • 更新了,新的错误截图也在里面
猜你喜欢
  • 2017-04-20
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多