javascript - 如何使用 React Hooks 获取 Antd Form 的值?

标签 javascript reactjs react-hooks

下面是TextareaItem antd-mobile的例子,
我想用 React Hooks 重写它,
这是我的半成品代码:

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;

问题:
1、如何用React Hooks获取TextareaItem的值?我会在获取这些值后发送ajax请求。有一个自定义钩子(Hook)react-use-form-state , 但它作用于 html 表单,如何在 Antd 表单上做同样的事情?

2、如何修改函数组件中的this.autoFocusInst.focus();这句?

最佳答案

为了使用 ref 你可以使用 useRef 钩子(Hook)。通过提供第二个参数作为空数组,还可以使 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 获取值。

关于javascript - 如何使用 React Hooks 获取 Antd Form 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53742033/

相关文章:

javascript - jquery 在同一函数中将两个 div 交叉淡入淡出在一起

javascript - React 是否去掉了关键属性

reactjs - 使用 create-react-app 获取开放图元数据

reactjs - react 钩子(Hook) : Display global spinner using axios interceptor?

reactjs - 当依赖之一是来自 useContext 的函数时,useEffect 中的 InfiniteLoop

javascript - 使用Jquery如何添加第一个子元素的背景颜色

javascript - jQuery Animate 在滚动时不起作用

javascript - Webpack 包 4.41.2 安全问题,因为 terser-webpack-plugin 使用了 serialize-javascript 版本 1.9.1

javascript - ReactJS:在 Modal 中动态加载组件的最佳方式

reactjs - 使用 React Hooks 进行依赖注入(inject)