首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Formikl / Yup字段有效后如何执行自定义函数

使用Formikl / Yup字段有效后如何执行自定义函数
EN

Stack Overflow用户
提问于 2018-08-31 20:08:42
回答 2查看 860关注 0票数 0

我想在字段生效时执行自定义函数吗?

就像这样..。

<Field name="postal-code" onValid={...} />

原因是,一旦用户输入有效的邮政编码,我想让make ( get )从API获取地址

EN

回答 2

Stack Overflow用户

发布于 2018-08-31 20:23:59

可以在组件类内或组件外部定义自定义函数。

代码语言:javascript
复制
// outside the component (best suited for functional component)
const onValidFn = () => {
 // perform action
}
// inside the component (best suited for stateful component)
onValidFn() {
 // perform action
}

如果要在onValidFn方法中访问this,则可以在构造函数中绑定this或使用公共类方法

代码语言:javascript
复制
onValidFn = () => {
  // perform action
  console.log(this)
}
代码语言:javascript
复制
// if your method is defined in outer scope
<Field name="postal-code" onValid={onValidFn} />

// if your method is defined in inner scope (inside class)
<Field name="postal-code" onValid={this.onValidFn} />
票数 0
EN

Stack Overflow用户

发布于 2018-09-03 12:49:34

你可以这样解决:

  • 拥有Loader组件,如果它获得一个URL,它将加载数据
  • 如果touched[fieldName] && !errors[fieldName],则将URL传递给此组件

Loader组件可以类似于

代码语言:javascript
复制
import { PureComponent } from 'react';
import PropTypes from 'prop-types';
import superagent from 'superagent'; // swap to your xhr library of choice

class Loader extends PureComponent {
  static propTypes = {
    url: PropTypes.string,
    onLoad: PropTypes.func,
    onError: PropTypes.func
  }

  static defaultProps = {
    url: '',
    onLoad: _ => {},
    onError: err => console.log(err)
  }

  state = {
    loading: false,
    data: null
  }

  componentDidMount() {
    this._isMounted = true;
    if (this.props.url) {
      this.getData()
    }
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.url !== this.props.url) {
      this.getData(nextProps)
    }
  }

  componentWillUnmount() {
    this._isMounted = false
  }

  getData = (props = this.props) => {
    const { url, onLoad, onError } = props;

    if (!url) {
      return
    }

    this.setState({ data: null, loading: true });

    const request = this.currentRequest = superagent.
      get(url).
      then(({ body: data }) => {
        if (this._isMounted && request === this.currentRequest) {
          this.setState({ data, loading: false }, _ => onLoad({ data }));
        }
      }).
      catch(err => {
        if (this._isMounted && request === this.currentRequest) {
          this.setState({ loading: false });
        }
        onError(err);
      });
  }

  render() {
    const { children } = this.props;
    return children instanceof Function ?
      children(this.state) :
      children || null;
  }
}

如果没有传递url,它就什么也不做。当url更改时,它会加载数据。

Formik呈现/子支持中的用法:

代码语言:javascript
复制
<Loader
  {...(touched[fieldName] && !errors[fieldName] && { url: URL_TO_FETCH })}
  onLoad={data => ...save data somewhere, etc.}
/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52122046

复制
相关文章

相似问题

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