Javascript 如何使用React钩子获取Antd表单的值?
下面是antd mobile的示例,Javascript 如何使用React钩子获取Antd表单的值?,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,下面是antd mobile的示例, 我想用React钩子重写它, 这是我的半成品代码: import React, { useState, useEffect} from "react" import { List, TextareaItem } from 'antd-mobile'; import { createForm } from 'rc-form'; function TextareaItemExample { useEffect(() => { //this
我想用React钩子重写它,
这是我的半成品代码:
import React, { useState, useEffect} from "react"
import { List, TextareaItem } from 'antd-mobile';
import { createForm } from 'rc-form';
function TextareaItemExample {
useEffect(() => {
//this.autoFocusInst.focus();
});
return (
<div>
<List renderHeader={() => 'Customize to focus'}>
<TextareaItem
title="title"
placeholder="auto focus in Alipay client"
data-seed="logId"
ref={el => this.autoFocusInst = el}
autoHeight
/>
<TextareaItem
title="content"
placeholder="click the button below to focus"
data-seed="logId"
autoHeight
/>
</List>
</div>
);
}
const TextareaItemExampleWrapper = createForm()(TextareaItemExample);
export default TextareaItemExampleWrapper;
import React,{useState,useffect}来自“React”
从“antd mobile”导入{List,TextareaItem};
从'rc form'导入{createForm};
函数TextareaItemExample{
useffect(()=>{
//this.autoFocusInst.focus();
});
返回(
“自定义到焦点”}>
this.autoFocusInst=el}
自动高度
/>
);
}
const TextareaItemExampleWrapper=createForm()(TextareaItemExample);
导出默认TextareaItemExampleWrapper;
问题:1、 如何使用React钩子获取
TextareaItem
的值?获取这些值后,我将发送ajax请求。有一个自定义钩子,但它作用于html表单,如何在Antd表单上执行相同的操作
2、 如何修改句子
this.autoFocusInst.focus()代码>在函数组件中?为了使用ref,可以使用useRef钩子。通过提供第二个参数作为空数组,还可以使useEffect的行为类似于componentDidMount
。使用受控的TextAreaItem,也可以获取状态中的值
import React, { useState, useEffect, useRef} from "react"
import { List, TextareaItem } from 'antd-mobile';
import { createForm } from 'rc-form';
function TextareaItemExample {
const [title, setTitle] = useState();
const [content, setContent] = useState();
const handleTitleChange = (value) => {
setTitle(value);
}
const handleContentChange = (value) => {
setContent(value)
}
const autoFocusInt = useRef();
useEffect(() => {
autoFocusInst.current.focus();
}, []);
return (
<div>
<List renderHeader={() => 'Customize to focus'}>
<TextareaItem
title="title"
value={title}
onChange={handleTitleChange}
placeholder="auto focus in Alipay client"
data-seed="logId"
ref={autoFocusInst}
autoHeight
/>
<TextareaItem
title="content"
value={content}
onChange={handleContentChange}
placeholder="click the button below to focus"
data-seed="logId"
autoHeight
/>
</List>
</div>
);
}
const TextareaItemExampleWrapper = createForm()(TextareaItemExample);
export default TextareaItemExampleWrapper;
import React,{useState,useffect,useRef}来自“React”
从“antd mobile”导入{List,TextareaItem};
从'rc form'导入{createForm};
函数TextareaItemExample{
const[title,setTitle]=useState();
const[content,setContent]=useState();
const handleitleChange=(值)=>{
设置标题(值);
}
常量handleContentChange=(值)=>{
设置内容(值)
}
const autoFocusInt=useRef();
useffect(()=>{
autoFocusInst.current.focus();
}, []);
返回(
“自定义到焦点”}>
);
}
const TextareaItemExampleWrapper=createForm()(TextareaItemExample);
导出默认TextareaItemExampleWrapper;
如果不将其作为受控输入,可能可以使用ref获得值