首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用获得Antd形式的值?

如何用获得Antd形式的值?
EN

Stack Overflow用户
提问于 2018-12-12 11:26:40
回答 1查看 6.6K关注 0票数 1

下面是antd的TextareaItem示例,

我想用重写它

这是我的半成品代码:

代码语言:javascript
复制
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();

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-12 12:45:53

为了利用ref,您可以使用useRef钩子。另外,可以通过提供第二个参数作为空数组,使useEffect的行为类似于componentDidMount。使用受控的TextAreaItem,您也可以获得状态的值。

代码语言:javascript
复制
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获取值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53742033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档