首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在函数之间传递变量?

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

Stack Overflow用户
提问于 2020-11-12 10:46:34
回答 1查看 52关注 0票数 0

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

  1. 如何将Fulloffset传递给scrollToRef函数?
  2. 如何在对象中声明道具,使它们在所有函数中都可用?我试图避免在每个函数arrayOfHeight

中声明类似于"const {}= this.props“的内容。

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

EN

回答 1

Stack Overflow用户

发布于 2020-11-12 11:01:01

将Fulloffset传递给scrollToRef

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

全球宣布道具

代码语言:javascript
复制
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;
    };
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64802423

复制
相关文章

相似问题

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