下面是antd的TextareaItem示例,
我想用重写它
这是我的半成品代码:
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;问题:
如何使用React获得TextareaItem的值?我将在获得那些values.There是自定义钩子反应-使用-形式-状态之后发送ajax请求,但是它对html表单起作用,如何在Antd表单上做同样的事情?
2在功能组件中如何修改句子this.autoFocusInst.focus();?
发布于 2018-12-12 12:45:53
为了利用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;如果不将其作为受控输入,则可能可以使用ref获取值。
https://stackoverflow.com/questions/53742033
复制相似问题