React + Redux:设置Redux状态或更改Redux状态后,页面未重新呈现

时间:2019-12-22 22:53:36

标签: javascript reactjs redux react-redux redux-saga

我正在执行api调用功能-getCountryList();使用redux saga获取我的国家列表 componentDidMount()生命周期方法。这是用于填充选择框内的国家/地区列表。

功能可以成功运行并设置redux状态,但是设置后,我的页面不会自动重新呈现。

我的减速器

import { SP_TYPES } from './sp.types';
const INITIAL_STATE = {
    countryList : [],
    stateList : [],
    errorMessage : "",
    isLoading : false
}
const ServiceProviderReducer = (state = INITIAL_STATE , action) => {
    switch(action.type)
    {
        case SP_TYPES.GET_COUNTRY_LIST_START :
            return {
                ...state,
                isLoading : true
            }
        case SP_TYPES.GET_COUNTRY_LIST_SUCCESS :
            return {
                ...state,
                countryList : action.payload,
                isLoading : false
            }
        case SP_TYPES.GET_COUNTRY_LIST_FAILURE :
            return {
                ...state,
                errorMessage : action.payload,
                isLoading : false
            }
        default :
            return state;
    }
}
export default ServiceProviderReducer;

我的传奇

import { takeLatest, call, put } from 'redux-saga/effects';
import { SP_TYPES } from './sp.types';
import axios from 'axios';
import {
    getCountryListSuccess,
    getCountryListFailure
} from './sp.actions.js';

const URL = process.env.REACT_APP_BASE_URL;

const getCountryListData = async () => {
    return axios({
        method : 'get',
        url : `${URL}/get_all_countries`
    });
}
export function* getCountryListAsync()
{
    try{
        const { data } = yield call(getCountryListData);
        if(data.status === 'success')
        {
            yield put(getCountryListSuccess(data.data))
        }
        else
        {
            yield put(getCountryListFailure())
        }
    }
    catch(er)
    {
        yield put(getCountryListFailure())
    }
}
export function* getCountryListStart()
{
    yield takeLatest(SP_TYPES.GET_COUNTRY_LIST_START,getCountryListAsync);
}

我的组件

import React from 'react';
import './add-service-provider-form.styles.scss';
import { withFormik, Form, Field } from 'formik';
import * as yup from 'yup';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { getCountryListStart } from '../../redux/super-admin-section/SP-realted/sp.actions';
import { selectCountryList } from '../../redux/super-admin-section/SP-realted/sp.selectors';

class AddServiceProviderForm extends React.Component {
    componentDidMount()
    {
        const { getCountryList, countryList } = this.props;
        console.log('countryList', countryList);
        getCountryList();

    }
    render() {
        const {  errors, touched, isSubmitting } = this.props;
        return (
            <Form>

                <div className="form-group">
                    <Field type="text" className="form-control" placeholder="Name" name="name" />
                    { touched.name && errors.name && <p className="text-danger">{errors.name}</p> }
                </div>
                <div className="form-group">
                    <Field type="text" className="form-control" placeholder="Address One" name="addressOne" />
                    { touched.addressOne && errors.addressOne && <p className="text-danger">{errors.addressOne}</p> }
                </div>
                <div className="form-group">
                    <Field type="text" className="form-control" placeholder="Address Two" name="addressTwo" />
                    { touched.addressTwo && errors.addressTwo && <p className="text-danger">{errors.addressTwo}</p> }
                </div>
                <div className="form-group">
                    <Field type="text" className="form-control" placeholder="City" name="city" />
                    { touched.city && errors.city && <p className="text-danger">{errors.city}</p> }
                </div>
                <div className="form-group">
                    <Field name="country" className="form-control" as="select">
                        <option value="1">Test</option>
                        <option value="2">Test 2</option>
                    </Field>
                    { touched.country && errors.country && <p className="text-danger">{errors.country}</p> }
                </div>
                <div className="form-group">
                    <Field name="state" className="form-control" as="select">
                        <option value="1">Test 11</option>
                        <option value="2">Test 22</option>
                    </Field>
                    { touched.state && errors.state && <p className="text-danger">{errors.state}</p> }
                </div>
                <div className="form-group">
                    <Field type="text" className="form-control" placeholder="zip" name="zip" />
                    { touched.zip && errors.zip && <p className="text-danger">{errors.zip}</p> }
                </div>

                <div className="form-group">
                    <Field type="number" className="form-control" placeholder="Phone Number" name="phoneNumber" />
                    { touched.phoneNumber && errors.phoneNumber && <p className="text-danger">{errors.phoneNumber}</p> }
                </div>
                <div className="form-group">
                    <Field type="email" className="form-control" placeholder="Email" name="email" />
                    { touched.email && errors.email && <p className="text-danger">{errors.email}</p> }
                </div>
                <div className="form-group">
                    <input type="submit" className="btn btn-primary form-control" disabled={isSubmitting}/>
                </div>
            </Form>
        );
    }
}
const options = {
    mapPropsToValues() {
        return {
            name: "",
            addressOne: "",
            addressTwo: "",
            city: "",
            country: "",
            state: "",
            zip: "",
            phoneNumber: "",
            email: "",
        }
    },
    handleSubmit({ resetForm, setErrors, setSubmitting} ) {

    },
    validationSchema : yup.object().shape({
        name : yup.string().required('Name is required'),
        addressOne: yup.string().required('Address One is required'),
        addressTwo: yup.string().required('Address two is required'),
        city: yup.string().required('City is required'),
        country: yup.string().required('Country is required'),
        state: yup.string().required('State is required'),
        zip: yup.string().required('ZIP code is required'),
        phoneNumber: yup.string().required('Phone Number is required'),
        email: yup.string().email('Email is not valid').required('Email is required'),
    })
}

const mapDispatchToProps = dispatch => {
    return {
        getCountryList : () => dispatch(getCountryListStart())
    }
}
const mapStateToProps = createStructuredSelector({
    countryList : selectCountryList
})
export default connect(mapStateToProps, mapDispatchToProps)(withFormik(options)(AddServiceProviderForm));

国家列表redux成功设置Redux记录器屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:0)

componentDidMount上进行网络通话,并期望countryList中已更新的componentDidUpdate

componentDidMount()
{
  const { getCountryList } = this.props;
  getCountryList();
}

componentDidUpdate {
  const { countryList } = this.props;
  console.log('countryList', countryList);
}