这是代码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;
在其他组件中显示值非常简单的任何方式或示例?
每次更改SimpleForm
的字段值时,更新的值都会保存在Redux的商店中。
为了获得表单的值:
ComponentOne
与Redux的商店连接起来,以便能够访问Form的状态。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)