Reactjs 反应最终形式字段如何访问一些初始值?
假设我的应用程序组件如下所示:Reactjs 反应最终形式字段如何访问一些初始值?,reactjs,react-final-form,Reactjs,React Final Form,假设我的应用程序组件如下所示: import { Form } from "react-final-form"; myInitData = { firstName: "akira", lastName: "fubuki" } render() { return <Form initialValues={myInitData} onSubmit={this.handleSubmit}
import { Form } from "react-final-form";
myInitData = {
firstName: "akira",
lastName: "fubuki"
}
render() {
return
<Form
initialValues={myInitData}
onSubmit={this.handleSubmit}
validate={this.validate}
>
{({handleSubmit, submitting, values}) => (
<MyComponentOuter
someProp1={100}
someProp2={200}
/>
)}
</Form>
}
class MyComponentInner extends Component {
componentDidMount() {
console.log(????) //how do i get access to firstName here..?
}
render() {
return (
<label>first name</label>
<Field
name="firstName"
component="input"
type="text"
/>
)
}
}
从“react final Form”导入{Form};
myInitData={
名字:“akira”,
姓氏:“fubuki”
}
render(){
返回
{({handleSubmit,submiting,values})=>(
)}
}
此外,我在MyComponentOuter中有一些组件mycomponentnner,因此层次结构是App=>MyComponentOuter=>mycomponentnner
myComponentNNER看起来像:
import { Form } from "react-final-form";
myInitData = {
firstName: "akira",
lastName: "fubuki"
}
render() {
return
<Form
initialValues={myInitData}
onSubmit={this.handleSubmit}
validate={this.validate}
>
{({handleSubmit, submitting, values}) => (
<MyComponentOuter
someProp1={100}
someProp2={200}
/>
)}
</Form>
}
class MyComponentInner extends Component {
componentDidMount() {
console.log(????) //how do i get access to firstName here..?
}
render() {
return (
<label>first name</label>
<Field
name="firstName"
component="input"
type="text"
/>
)
}
}
类myComponentNNER扩展组件{
componentDidMount(){
console.log(????//如何在此处访问firstName。。?
}
render(){
返回(
名字
)
}
}
我的问题是:
a) react最终表单的组件如何自动访问“firstName”属性?(没有明确地向其传递任何道具)
b) 我想在myComponentNNER中访问相同的firstName值;例如,如果我想console.log firstName.a)它使用的语法是什么。
组件持有一个最终表单
对象,该对象为您管理所有表单状态,
组件使用上下文
与表单状态对话
b) 像这样的东西就行了。为字段
提供的渲染功能与所有这些功能一起提供
{
({input}/*FieldRenderOps*/)=>{
console.log(input.value)
返回null//render nothing(这是一个渲染函数)
}
}
非常感谢您的回复。我只是想澄清一下,所以我无法像我在componentDidMount()内的问题中所做的那样,在外部提取底层的firstName值?