简体   繁体   English

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

[英]how do I pass variables between functions?

Good afternoon, friends.下午好,朋友们。 I have two questions.我有两个问题。

  1. How can I pass Fulloffset to the scrollToRef function?如何将 Fulloffset 传递给 scrollToRef 函数?
  2. How can I declare props in an object so that they are available in all functions?如何在对象中声明 props 以便它们在所有函数中可用? I'm trying to avoid declaring something like "const { arrayOfHeight } = this.props" in every function我试图避免在每个函数中声明类似“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, }); } };

Pass Fulloffset to the scrollToRef将 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,
        });
      }
    };

Declare props globally全局声明 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,
      };
     // initialize here
     this.arrayOfHeight = props.arrayOfHeight;
    }

    componentDidUpdate() {
      this.existError();
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM