React-redux mapStateToProps values not updated











up vote
0
down vote

favorite












I have a weird scenario in which inside my redux dev-tools I can see that the redux state has been updated but that value is not being reflected in the component which is connected to that.



So I have a component CreateEnvironment like



export interface StateProps {
environment: EnvironmentStep;
selectProduct: SelectProductStep;
eula: EULAStep;
license: LicenseStep;
infrastructure: InfrastructureStep;
network: NetworkStep;
certificate: CertificateStep;
products: any;
}

export interface DispatchProps {
onFormValuesUpdate: (key: string, value: any) => void;
}

type Props = StateProps & DispatchProps;

interface State {
dashboard: boolean;
}

class CreateEnvironment extends Component<Props, State> {
private page: RefObject<any>;
constructor(props: Props) {
super(props);
this.state = { dashboard: false };
this.page = React.createRef();
}
public render() {
console.log("Hi"); //tslint:disable-line
if (this.state.dashboard) {
return <Redirect to="/dashboard" />;
}
const {
environment,
eula,
selectProduct,
infrastructure,
network,
license,
certificate,
onFormValuesUpdate
} = this.props;
return (
<Fragment>
<h2>Create Environment</h2>
<div style={{ height: '100%', paddingBottom: '30px' }}>
<WorkFlow
orientation="vertical"
onNext={this.onNext}
onFinish={this.onFinish}
>
<WorkFlowPage pageTitle="Environment">
<Environment
environment={environment}
onUpdate={onFormValuesUpdate}
ref={this.page}
/>
</WorkFlowPage>
<WorkFlowPage pageTitle="Products & Solutions">
<SelectProduct
ref={this.page}
selectProduct={selectProduct}
onUpdate={onFormValuesUpdate}
/>
</WorkFlowPage>
<WorkFlowPage
pageNavTitle="EULA"
pageTitle="End User License Agreement Details"
>
<Eula eula={eula} ref={this.page} onUpdate={onFormValuesUpdate} />
</WorkFlowPage>
<WorkFlowPage pageTitle="License">
<License
license={license}
ref={this.page}
onUpdate={onFormValuesUpdate}
/>
</WorkFlowPage>
<WorkFlowPage pageTitle="Infrastructure">
<Infrastructure
infrastructure={infrastructure}
ref={this.page}
onUpdate={onFormValuesUpdate}
/>
</WorkFlowPage>
<WorkFlowPage pageTitle="Network">
<Network
network={network}
ref={this.page}
onUpdate={onFormValuesUpdate}
/>
</WorkFlowPage>
<WorkFlowPage pageTitle="Certificate">
<Certificate
certificate={certificate}
ref={this.page}
onUpdate={onFormValuesUpdate}
/>
</WorkFlowPage>
<WorkFlowPage pageTitle="Products">I am products step</WorkFlowPage>
<WorkFlowPage pageTitle="Summary">I am summary step</WorkFlowPage>
<WorkFlowButton role="previous">Back</WorkFlowButton>
<WorkFlowButton role="next">Next</WorkFlowButton>
<WorkFlowButton role="finish">Finish</WorkFlowButton>
</WorkFlow>
</div>
</Fragment>
);
}

private onNext = () => {
if (this.page.current) {
this.page.current.handleSubmit();
}
}

private onFinish = () => {
this.setState({
dashboard: true
});
}
}

export default CreateEnvironment;


The corresponding container looks like:



import { connect } from 'react-redux';
import { Dispatch } from 'redux';
import {
CreateEnvironmentAction,
updateEnvironmentWorkflow
} from '../actions/CreateEnvironment';
import {
CreateEnvironment,
DispatchProps,
StateProps
} from '../components/create-environment';
import { StoreState } from '../types';
export const mapStateToProps = ({
createEnvironment
}: StoreState): StateProps => ({
...createEnvironment
});

export const mapDispatchToProps = (
dispatch: Dispatch<CreateEnvironmentAction>
): DispatchProps => ({
onFormValuesUpdate: (key: string, values: any) => {
dispatch(updateEnvironmentWorkflow(key, values));
}
});

export default connect<StateProps, DispatchProps>(
mapStateToProps,
mapDispatchToProps
)(CreateEnvironment);


All the components inside WorkflowPage like Environment are forms which use the Formik pattern.
A sample component is:



interface Props {
certificate?: CertificateStep;
onUpdate?: (key: string, value: any) => void;
}
class Certificate extends Component<Props> {
private submitForm: (
e?: React.FormEvent<HTMLFormElement> | undefined
) => void;
public render() {
const { formValues } = this.props.certificate!;
console.log(formValues); //tslint:disable-line
return (
<Form
className="license"
type="horizontal"
initialValues={formValues}
onSubmit={this.onSubmit}
>
{formProps => {
this.submitForm = formProps.handleSubmit;
return (
<Fragment>
<CheckboxField
name="productSpecific"
id="productSpecific"
type="toggle"
>
Provide Product Specific Certificate
</CheckboxField>
<SelectField name="certificate" label="Certificate">
<SelectOption value="">---Select Certificate---</SelectOption>
</SelectField>
</Fragment>
);
}}
</Form>
);
}

public handleSubmit = () => {
this.submitForm();
}

private onSubmit = (values: FormValues, actions: FormActions) => {
this.props.onUpdate!('certificate', values);
actions.setSubmitting(false);
}
}

export default Certificate;


On clicking the next WorkflowButton the redux state is updated by passing an action updateEnvironmentWorkflow. The action is like:



export interface UpdateEnvironmentWorkflow {
type: Constants.UPDATE_ENVIRONMENT_WORKFLOW;
payload: {
key: string;
value: any;
};
}

export type CreateEnvironmentAction = UpdateEnvironmentWorkflow;

export const updateEnvironmentWorkflow = (
key: string,
value: any
): UpdateEnvironmentWorkflow => ({
payload: { key, value },
type: Constants.UPDATE_ENVIRONMENT_WORKFLOW
});


And my reducer is like:



const createEnvironment = (
state: CreateEnvironment = initialState.createEnvironment,
action: CreateEnvironmentAction
) => {
switch (action.type) {
case Constants.UPDATE_ENVIRONMENT_WORKFLOW:
return {
...state,
[action.payload.key]: {
...state[action.payload.key],
formValues: action.payload.value
}
};
default:
return state;
}
};

export default createEnvironment;


Now the weird part is that if I click the next button the redux state is updated with new values. If I navigate to some other page and come back to create environment my form values are showing the updated values from the redux store state.



BUT if I just hit the previous button my state values are not showing updated values.
The console.log(formValues) inside render() of Certificate is showing initial values only.



Any help will be appreciated. Thanks.



Also initialValues of Form is Formik initialValues. It maybe a formik issue as well










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a weird scenario in which inside my redux dev-tools I can see that the redux state has been updated but that value is not being reflected in the component which is connected to that.



    So I have a component CreateEnvironment like



    export interface StateProps {
    environment: EnvironmentStep;
    selectProduct: SelectProductStep;
    eula: EULAStep;
    license: LicenseStep;
    infrastructure: InfrastructureStep;
    network: NetworkStep;
    certificate: CertificateStep;
    products: any;
    }

    export interface DispatchProps {
    onFormValuesUpdate: (key: string, value: any) => void;
    }

    type Props = StateProps & DispatchProps;

    interface State {
    dashboard: boolean;
    }

    class CreateEnvironment extends Component<Props, State> {
    private page: RefObject<any>;
    constructor(props: Props) {
    super(props);
    this.state = { dashboard: false };
    this.page = React.createRef();
    }
    public render() {
    console.log("Hi"); //tslint:disable-line
    if (this.state.dashboard) {
    return <Redirect to="/dashboard" />;
    }
    const {
    environment,
    eula,
    selectProduct,
    infrastructure,
    network,
    license,
    certificate,
    onFormValuesUpdate
    } = this.props;
    return (
    <Fragment>
    <h2>Create Environment</h2>
    <div style={{ height: '100%', paddingBottom: '30px' }}>
    <WorkFlow
    orientation="vertical"
    onNext={this.onNext}
    onFinish={this.onFinish}
    >
    <WorkFlowPage pageTitle="Environment">
    <Environment
    environment={environment}
    onUpdate={onFormValuesUpdate}
    ref={this.page}
    />
    </WorkFlowPage>
    <WorkFlowPage pageTitle="Products & Solutions">
    <SelectProduct
    ref={this.page}
    selectProduct={selectProduct}
    onUpdate={onFormValuesUpdate}
    />
    </WorkFlowPage>
    <WorkFlowPage
    pageNavTitle="EULA"
    pageTitle="End User License Agreement Details"
    >
    <Eula eula={eula} ref={this.page} onUpdate={onFormValuesUpdate} />
    </WorkFlowPage>
    <WorkFlowPage pageTitle="License">
    <License
    license={license}
    ref={this.page}
    onUpdate={onFormValuesUpdate}
    />
    </WorkFlowPage>
    <WorkFlowPage pageTitle="Infrastructure">
    <Infrastructure
    infrastructure={infrastructure}
    ref={this.page}
    onUpdate={onFormValuesUpdate}
    />
    </WorkFlowPage>
    <WorkFlowPage pageTitle="Network">
    <Network
    network={network}
    ref={this.page}
    onUpdate={onFormValuesUpdate}
    />
    </WorkFlowPage>
    <WorkFlowPage pageTitle="Certificate">
    <Certificate
    certificate={certificate}
    ref={this.page}
    onUpdate={onFormValuesUpdate}
    />
    </WorkFlowPage>
    <WorkFlowPage pageTitle="Products">I am products step</WorkFlowPage>
    <WorkFlowPage pageTitle="Summary">I am summary step</WorkFlowPage>
    <WorkFlowButton role="previous">Back</WorkFlowButton>
    <WorkFlowButton role="next">Next</WorkFlowButton>
    <WorkFlowButton role="finish">Finish</WorkFlowButton>
    </WorkFlow>
    </div>
    </Fragment>
    );
    }

    private onNext = () => {
    if (this.page.current) {
    this.page.current.handleSubmit();
    }
    }

    private onFinish = () => {
    this.setState({
    dashboard: true
    });
    }
    }

    export default CreateEnvironment;


    The corresponding container looks like:



    import { connect } from 'react-redux';
    import { Dispatch } from 'redux';
    import {
    CreateEnvironmentAction,
    updateEnvironmentWorkflow
    } from '../actions/CreateEnvironment';
    import {
    CreateEnvironment,
    DispatchProps,
    StateProps
    } from '../components/create-environment';
    import { StoreState } from '../types';
    export const mapStateToProps = ({
    createEnvironment
    }: StoreState): StateProps => ({
    ...createEnvironment
    });

    export const mapDispatchToProps = (
    dispatch: Dispatch<CreateEnvironmentAction>
    ): DispatchProps => ({
    onFormValuesUpdate: (key: string, values: any) => {
    dispatch(updateEnvironmentWorkflow(key, values));
    }
    });

    export default connect<StateProps, DispatchProps>(
    mapStateToProps,
    mapDispatchToProps
    )(CreateEnvironment);


    All the components inside WorkflowPage like Environment are forms which use the Formik pattern.
    A sample component is:



    interface Props {
    certificate?: CertificateStep;
    onUpdate?: (key: string, value: any) => void;
    }
    class Certificate extends Component<Props> {
    private submitForm: (
    e?: React.FormEvent<HTMLFormElement> | undefined
    ) => void;
    public render() {
    const { formValues } = this.props.certificate!;
    console.log(formValues); //tslint:disable-line
    return (
    <Form
    className="license"
    type="horizontal"
    initialValues={formValues}
    onSubmit={this.onSubmit}
    >
    {formProps => {
    this.submitForm = formProps.handleSubmit;
    return (
    <Fragment>
    <CheckboxField
    name="productSpecific"
    id="productSpecific"
    type="toggle"
    >
    Provide Product Specific Certificate
    </CheckboxField>
    <SelectField name="certificate" label="Certificate">
    <SelectOption value="">---Select Certificate---</SelectOption>
    </SelectField>
    </Fragment>
    );
    }}
    </Form>
    );
    }

    public handleSubmit = () => {
    this.submitForm();
    }

    private onSubmit = (values: FormValues, actions: FormActions) => {
    this.props.onUpdate!('certificate', values);
    actions.setSubmitting(false);
    }
    }

    export default Certificate;


    On clicking the next WorkflowButton the redux state is updated by passing an action updateEnvironmentWorkflow. The action is like:



    export interface UpdateEnvironmentWorkflow {
    type: Constants.UPDATE_ENVIRONMENT_WORKFLOW;
    payload: {
    key: string;
    value: any;
    };
    }

    export type CreateEnvironmentAction = UpdateEnvironmentWorkflow;

    export const updateEnvironmentWorkflow = (
    key: string,
    value: any
    ): UpdateEnvironmentWorkflow => ({
    payload: { key, value },
    type: Constants.UPDATE_ENVIRONMENT_WORKFLOW
    });


    And my reducer is like:



    const createEnvironment = (
    state: CreateEnvironment = initialState.createEnvironment,
    action: CreateEnvironmentAction
    ) => {
    switch (action.type) {
    case Constants.UPDATE_ENVIRONMENT_WORKFLOW:
    return {
    ...state,
    [action.payload.key]: {
    ...state[action.payload.key],
    formValues: action.payload.value
    }
    };
    default:
    return state;
    }
    };

    export default createEnvironment;


    Now the weird part is that if I click the next button the redux state is updated with new values. If I navigate to some other page and come back to create environment my form values are showing the updated values from the redux store state.



    BUT if I just hit the previous button my state values are not showing updated values.
    The console.log(formValues) inside render() of Certificate is showing initial values only.



    Any help will be appreciated. Thanks.



    Also initialValues of Form is Formik initialValues. It maybe a formik issue as well










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a weird scenario in which inside my redux dev-tools I can see that the redux state has been updated but that value is not being reflected in the component which is connected to that.



      So I have a component CreateEnvironment like



      export interface StateProps {
      environment: EnvironmentStep;
      selectProduct: SelectProductStep;
      eula: EULAStep;
      license: LicenseStep;
      infrastructure: InfrastructureStep;
      network: NetworkStep;
      certificate: CertificateStep;
      products: any;
      }

      export interface DispatchProps {
      onFormValuesUpdate: (key: string, value: any) => void;
      }

      type Props = StateProps & DispatchProps;

      interface State {
      dashboard: boolean;
      }

      class CreateEnvironment extends Component<Props, State> {
      private page: RefObject<any>;
      constructor(props: Props) {
      super(props);
      this.state = { dashboard: false };
      this.page = React.createRef();
      }
      public render() {
      console.log("Hi"); //tslint:disable-line
      if (this.state.dashboard) {
      return <Redirect to="/dashboard" />;
      }
      const {
      environment,
      eula,
      selectProduct,
      infrastructure,
      network,
      license,
      certificate,
      onFormValuesUpdate
      } = this.props;
      return (
      <Fragment>
      <h2>Create Environment</h2>
      <div style={{ height: '100%', paddingBottom: '30px' }}>
      <WorkFlow
      orientation="vertical"
      onNext={this.onNext}
      onFinish={this.onFinish}
      >
      <WorkFlowPage pageTitle="Environment">
      <Environment
      environment={environment}
      onUpdate={onFormValuesUpdate}
      ref={this.page}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Products & Solutions">
      <SelectProduct
      ref={this.page}
      selectProduct={selectProduct}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage
      pageNavTitle="EULA"
      pageTitle="End User License Agreement Details"
      >
      <Eula eula={eula} ref={this.page} onUpdate={onFormValuesUpdate} />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="License">
      <License
      license={license}
      ref={this.page}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Infrastructure">
      <Infrastructure
      infrastructure={infrastructure}
      ref={this.page}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Network">
      <Network
      network={network}
      ref={this.page}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Certificate">
      <Certificate
      certificate={certificate}
      ref={this.page}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Products">I am products step</WorkFlowPage>
      <WorkFlowPage pageTitle="Summary">I am summary step</WorkFlowPage>
      <WorkFlowButton role="previous">Back</WorkFlowButton>
      <WorkFlowButton role="next">Next</WorkFlowButton>
      <WorkFlowButton role="finish">Finish</WorkFlowButton>
      </WorkFlow>
      </div>
      </Fragment>
      );
      }

      private onNext = () => {
      if (this.page.current) {
      this.page.current.handleSubmit();
      }
      }

      private onFinish = () => {
      this.setState({
      dashboard: true
      });
      }
      }

      export default CreateEnvironment;


      The corresponding container looks like:



      import { connect } from 'react-redux';
      import { Dispatch } from 'redux';
      import {
      CreateEnvironmentAction,
      updateEnvironmentWorkflow
      } from '../actions/CreateEnvironment';
      import {
      CreateEnvironment,
      DispatchProps,
      StateProps
      } from '../components/create-environment';
      import { StoreState } from '../types';
      export const mapStateToProps = ({
      createEnvironment
      }: StoreState): StateProps => ({
      ...createEnvironment
      });

      export const mapDispatchToProps = (
      dispatch: Dispatch<CreateEnvironmentAction>
      ): DispatchProps => ({
      onFormValuesUpdate: (key: string, values: any) => {
      dispatch(updateEnvironmentWorkflow(key, values));
      }
      });

      export default connect<StateProps, DispatchProps>(
      mapStateToProps,
      mapDispatchToProps
      )(CreateEnvironment);


      All the components inside WorkflowPage like Environment are forms which use the Formik pattern.
      A sample component is:



      interface Props {
      certificate?: CertificateStep;
      onUpdate?: (key: string, value: any) => void;
      }
      class Certificate extends Component<Props> {
      private submitForm: (
      e?: React.FormEvent<HTMLFormElement> | undefined
      ) => void;
      public render() {
      const { formValues } = this.props.certificate!;
      console.log(formValues); //tslint:disable-line
      return (
      <Form
      className="license"
      type="horizontal"
      initialValues={formValues}
      onSubmit={this.onSubmit}
      >
      {formProps => {
      this.submitForm = formProps.handleSubmit;
      return (
      <Fragment>
      <CheckboxField
      name="productSpecific"
      id="productSpecific"
      type="toggle"
      >
      Provide Product Specific Certificate
      </CheckboxField>
      <SelectField name="certificate" label="Certificate">
      <SelectOption value="">---Select Certificate---</SelectOption>
      </SelectField>
      </Fragment>
      );
      }}
      </Form>
      );
      }

      public handleSubmit = () => {
      this.submitForm();
      }

      private onSubmit = (values: FormValues, actions: FormActions) => {
      this.props.onUpdate!('certificate', values);
      actions.setSubmitting(false);
      }
      }

      export default Certificate;


      On clicking the next WorkflowButton the redux state is updated by passing an action updateEnvironmentWorkflow. The action is like:



      export interface UpdateEnvironmentWorkflow {
      type: Constants.UPDATE_ENVIRONMENT_WORKFLOW;
      payload: {
      key: string;
      value: any;
      };
      }

      export type CreateEnvironmentAction = UpdateEnvironmentWorkflow;

      export const updateEnvironmentWorkflow = (
      key: string,
      value: any
      ): UpdateEnvironmentWorkflow => ({
      payload: { key, value },
      type: Constants.UPDATE_ENVIRONMENT_WORKFLOW
      });


      And my reducer is like:



      const createEnvironment = (
      state: CreateEnvironment = initialState.createEnvironment,
      action: CreateEnvironmentAction
      ) => {
      switch (action.type) {
      case Constants.UPDATE_ENVIRONMENT_WORKFLOW:
      return {
      ...state,
      [action.payload.key]: {
      ...state[action.payload.key],
      formValues: action.payload.value
      }
      };
      default:
      return state;
      }
      };

      export default createEnvironment;


      Now the weird part is that if I click the next button the redux state is updated with new values. If I navigate to some other page and come back to create environment my form values are showing the updated values from the redux store state.



      BUT if I just hit the previous button my state values are not showing updated values.
      The console.log(formValues) inside render() of Certificate is showing initial values only.



      Any help will be appreciated. Thanks.



      Also initialValues of Form is Formik initialValues. It maybe a formik issue as well










      share|improve this question















      I have a weird scenario in which inside my redux dev-tools I can see that the redux state has been updated but that value is not being reflected in the component which is connected to that.



      So I have a component CreateEnvironment like



      export interface StateProps {
      environment: EnvironmentStep;
      selectProduct: SelectProductStep;
      eula: EULAStep;
      license: LicenseStep;
      infrastructure: InfrastructureStep;
      network: NetworkStep;
      certificate: CertificateStep;
      products: any;
      }

      export interface DispatchProps {
      onFormValuesUpdate: (key: string, value: any) => void;
      }

      type Props = StateProps & DispatchProps;

      interface State {
      dashboard: boolean;
      }

      class CreateEnvironment extends Component<Props, State> {
      private page: RefObject<any>;
      constructor(props: Props) {
      super(props);
      this.state = { dashboard: false };
      this.page = React.createRef();
      }
      public render() {
      console.log("Hi"); //tslint:disable-line
      if (this.state.dashboard) {
      return <Redirect to="/dashboard" />;
      }
      const {
      environment,
      eula,
      selectProduct,
      infrastructure,
      network,
      license,
      certificate,
      onFormValuesUpdate
      } = this.props;
      return (
      <Fragment>
      <h2>Create Environment</h2>
      <div style={{ height: '100%', paddingBottom: '30px' }}>
      <WorkFlow
      orientation="vertical"
      onNext={this.onNext}
      onFinish={this.onFinish}
      >
      <WorkFlowPage pageTitle="Environment">
      <Environment
      environment={environment}
      onUpdate={onFormValuesUpdate}
      ref={this.page}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Products & Solutions">
      <SelectProduct
      ref={this.page}
      selectProduct={selectProduct}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage
      pageNavTitle="EULA"
      pageTitle="End User License Agreement Details"
      >
      <Eula eula={eula} ref={this.page} onUpdate={onFormValuesUpdate} />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="License">
      <License
      license={license}
      ref={this.page}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Infrastructure">
      <Infrastructure
      infrastructure={infrastructure}
      ref={this.page}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Network">
      <Network
      network={network}
      ref={this.page}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Certificate">
      <Certificate
      certificate={certificate}
      ref={this.page}
      onUpdate={onFormValuesUpdate}
      />
      </WorkFlowPage>
      <WorkFlowPage pageTitle="Products">I am products step</WorkFlowPage>
      <WorkFlowPage pageTitle="Summary">I am summary step</WorkFlowPage>
      <WorkFlowButton role="previous">Back</WorkFlowButton>
      <WorkFlowButton role="next">Next</WorkFlowButton>
      <WorkFlowButton role="finish">Finish</WorkFlowButton>
      </WorkFlow>
      </div>
      </Fragment>
      );
      }

      private onNext = () => {
      if (this.page.current) {
      this.page.current.handleSubmit();
      }
      }

      private onFinish = () => {
      this.setState({
      dashboard: true
      });
      }
      }

      export default CreateEnvironment;


      The corresponding container looks like:



      import { connect } from 'react-redux';
      import { Dispatch } from 'redux';
      import {
      CreateEnvironmentAction,
      updateEnvironmentWorkflow
      } from '../actions/CreateEnvironment';
      import {
      CreateEnvironment,
      DispatchProps,
      StateProps
      } from '../components/create-environment';
      import { StoreState } from '../types';
      export const mapStateToProps = ({
      createEnvironment
      }: StoreState): StateProps => ({
      ...createEnvironment
      });

      export const mapDispatchToProps = (
      dispatch: Dispatch<CreateEnvironmentAction>
      ): DispatchProps => ({
      onFormValuesUpdate: (key: string, values: any) => {
      dispatch(updateEnvironmentWorkflow(key, values));
      }
      });

      export default connect<StateProps, DispatchProps>(
      mapStateToProps,
      mapDispatchToProps
      )(CreateEnvironment);


      All the components inside WorkflowPage like Environment are forms which use the Formik pattern.
      A sample component is:



      interface Props {
      certificate?: CertificateStep;
      onUpdate?: (key: string, value: any) => void;
      }
      class Certificate extends Component<Props> {
      private submitForm: (
      e?: React.FormEvent<HTMLFormElement> | undefined
      ) => void;
      public render() {
      const { formValues } = this.props.certificate!;
      console.log(formValues); //tslint:disable-line
      return (
      <Form
      className="license"
      type="horizontal"
      initialValues={formValues}
      onSubmit={this.onSubmit}
      >
      {formProps => {
      this.submitForm = formProps.handleSubmit;
      return (
      <Fragment>
      <CheckboxField
      name="productSpecific"
      id="productSpecific"
      type="toggle"
      >
      Provide Product Specific Certificate
      </CheckboxField>
      <SelectField name="certificate" label="Certificate">
      <SelectOption value="">---Select Certificate---</SelectOption>
      </SelectField>
      </Fragment>
      );
      }}
      </Form>
      );
      }

      public handleSubmit = () => {
      this.submitForm();
      }

      private onSubmit = (values: FormValues, actions: FormActions) => {
      this.props.onUpdate!('certificate', values);
      actions.setSubmitting(false);
      }
      }

      export default Certificate;


      On clicking the next WorkflowButton the redux state is updated by passing an action updateEnvironmentWorkflow. The action is like:



      export interface UpdateEnvironmentWorkflow {
      type: Constants.UPDATE_ENVIRONMENT_WORKFLOW;
      payload: {
      key: string;
      value: any;
      };
      }

      export type CreateEnvironmentAction = UpdateEnvironmentWorkflow;

      export const updateEnvironmentWorkflow = (
      key: string,
      value: any
      ): UpdateEnvironmentWorkflow => ({
      payload: { key, value },
      type: Constants.UPDATE_ENVIRONMENT_WORKFLOW
      });


      And my reducer is like:



      const createEnvironment = (
      state: CreateEnvironment = initialState.createEnvironment,
      action: CreateEnvironmentAction
      ) => {
      switch (action.type) {
      case Constants.UPDATE_ENVIRONMENT_WORKFLOW:
      return {
      ...state,
      [action.payload.key]: {
      ...state[action.payload.key],
      formValues: action.payload.value
      }
      };
      default:
      return state;
      }
      };

      export default createEnvironment;


      Now the weird part is that if I click the next button the redux state is updated with new values. If I navigate to some other page and come back to create environment my form values are showing the updated values from the redux store state.



      BUT if I just hit the previous button my state values are not showing updated values.
      The console.log(formValues) inside render() of Certificate is showing initial values only.



      Any help will be appreciated. Thanks.



      Also initialValues of Form is Formik initialValues. It maybe a formik issue as well







      reactjs redux react-redux formik






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 16:39

























      asked Nov 10 at 16:11









      Varun Sharma

      106




      106





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240834%2freact-redux-mapstatetoprops-values-not-updated%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240834%2freact-redux-mapstatetoprops-values-not-updated%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python