微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在函数之间传递变量?

如何解决如何在函数之间传递变量?

朋友们,下午好。我有两个问题。

  1. 如何将Fulloffset传递给scrollToRef函数
  2. 如何在对象中声明道具,以便它们在所有功能中都可用?我试图避免在每个函数中声明类似“ const {arrayOfheight} = this.props”的内容
export function withScrollToFirstError(Component: t.form.Component): React.ComponentType {
  class ScrollToFirstErrorHOC extends PureComponent<OuterProps & PropsFromState,ComponentState> {
    constructor(props: OuterProps & PropsFromState) {
      super(props);
      this.state = {
        height: 0,offset: 0,};
    }

    componentDidUpdate() {
      this.existError();
    }

    existError = () => {
      const { currentFieldId,firstFieldId,arrayOfheight,fieldOffset,fieldErrors } = this.props;
      this.calculateCoordinate();
    };

    calculateCoordinate = () => {
        const fullOffset = offset + fieldOffset[0];
      this.scrollToRef();
    };

    scrollToRef = () => {
      if (this.props.reference) {
        this.props.reference.current.scrollTo({
          x: 0,y: 0,animated: true,});
      }
    };

解决方法

将Fulloffset传递给scrollToRef

calculateCoordinate = () => {
      const fullOffset = offset + fieldOffset[0];
      this.scrollToRef(fullOffset); // like this
    };

scrollToRef = (fullOffset) => {
      if (this.props.reference) {
        this.props.reference.current.scrollTo({
          x: 0,y: 0,animated: true,});
      }
    };

全局声明道具

export function withScrollToFirstError(Component: t.form.Component): React.ComponentType {
  class ScrollToFirstErrorHOC extends PureComponent<OuterProps & PropsFromState,ComponentState> {
    constructor(props: OuterProps & PropsFromState) {
      super(props);
      this.state = {
        height: 0,offset: 0,};
     // initialize here
     this.arrayOfHeight = props.arrayOfHeight;
    }

    componentDidUpdate() {
      this.existError();
    }

    existError = () => {
      // use here like 
      this.arrayOfHeight;
    };
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。