从其他组件实时更改redux-form读取值

问题描述 投票:1回答:1

这是代码https://codesandbox.io/s/7073llkj

这个简单的例子使用redux-form-website-template来显示值,我是新的反应和redux-form,我甚至不知道这是什么,我只是想在其他组件中读取非常简单的值。

import React, { Component } from "react";

class ComponentOne extends Component {
  render() {
    return (
      <div>
        <div>CompoenntOne: I wannna read the props.values.firstName here</div>
        <div>CompoenntOne: I wannna read the props.values.lastName here</div>
      </div>
    );
  }
}

export default ComponentOne;

在其他组件中显示值非常简单的任何方式或示例?

reactjs redux-form
1个回答
1
投票

每次更改SimpleForm的字段值时,更新的值都会保存在Redux的商店中。

为了获得表单的值:

  1. 您要做的第一件事就是将ComponentOne与Redux的商店连接起来,以便能够访问Form的状态。
  2. 其次,您可以查询商店并获取SimpleForm的值。

像这样的东西:

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { getFormValues } from 'redux-form'


class ComponentOne extends Component {
  render() {
    const { values } = this.props

    return (
      <div>
        <div>CompoenntOne: I wannna read the values.firstName here</div>
        <div>CompoenntOne: I wannna read the values.lastName here</div>
      </div>
    )
  }
}

const mapStateToProps = state => ({
  // 2. Secondly, you can query the Store and get the `SimpleForm`'s field value.
  values: getFormValues('simple')(state) || {}
})

// 1. First thing you have to do is to connect `ComponentOne` with the Redux's Store,
// in order to have access to the Form's state, because it's kept in the Store.
export default connect(mapStateToProps)(ComponentOne)
© www.soinside.com 2019 - 2024. All rights reserved.