简体   繁体   English

为什么我不能在render()里面调用我的function?

[英]Why can I not call my function inside the render ()?

I am very new using React and I have the following problem;我是使用 React 的新手,我遇到以下问题; I have defined a function out of the return () , and I am calling it afterwards so it shows only when the page is loaded.我在return ()之外定义了一个 function ,然后我在调用它,所以它只在页面加载时显示。

class Landing extends Component {
  render() {
    scrollbar = () => {
      const progress = document.getElementById("progressBar");
      const totalHeight = document.body.scrollHeight - window.innerHeight;
      window.onscroll = function () {
        const progressHeight = (window.pageYOffset / totalHeight) * 100;
        progress.style.height = progressHeight + "%";
      };
    };

    return (
      <div>
        <div id="progressBar"> {this.scrollbar()}</div>
        <div id="scrollPath"></div>
      </div>
    )
  }
}

The error that I get is:我得到的错误是:

"scrollbar is undefined" // Search for the keywords to learn more about each error. "scrollbar is undefined" // 搜索关键字以了解有关每个错误的更多信息。

Any help is much appreciated.任何帮助深表感谢。

Thank you!谢谢!

You must define the method (scrollbar) above the render method.您必须在渲染方法上方定义方法(滚动条)。

class Landing extends Component {
    scrollbar = () => {
      const progress = document.getElementById("progressBar");
      const totalHeight = document.body.scrollHeight - window.innerHeight;
      window.onscroll = function () {
        const progressHeight = (window.pageYOffset / totalHeight) * 100;
        progress.style.height = progressHeight + "%";
      };
    };

  render() {
    return (
      <>
        <div id="progressBar">{this.scrollbar}</div>
        <div id="scrollPath"></div>
      </>
    )
  }
}

I hope this solve your issue.我希望这能解决你的问题。 :) :)

First, you shouldn't be doing DOM manipulation like that when using react, so ideally you would rewrite the scrollbar function to be more inline with react.首先,您不应该在使用 react 时进行这样的 DOM 操作,因此理想情况下,您应该重写scrollbar function 以使其与 react 更加内联。

The error you are getting is because this is referring to the class, meaning this.scrollbar is looking for a class property called scrollbar.您得到的错误是因为this是指 class,这意味着this.scrollbar正在寻找一个名为滚动条的 class 属性。 You do not have a class property called scrollbar, but a function declared within the render method.您没有称为滚动条的 class 属性,但在render方法中声明了 function。

You have two options to solve this error.您有两种选择来解决此错误。

Reference the function instead of a class property:参考 function 而不是 class 属性:

<div id="progressBar">{scrollbar()}</div>

Change the function to be a class property:将 function 更改为 class 属性:

class Landing extends Component {
  scrollbar = () => {
    const progress = document.getElementById("progressBar");
    const totalHeight = document.body.scrollHeight - window.innerHeight;
    window.onscroll = function () {
      const progressHeight = (window.pageYOffset / totalHeight) * 100;
      progress.style.height = progressHeight + "%";
    };
  };

Once you do one of these, you still might see some unexpected behavior.一旦您执行其中一项操作,您仍然可能会看到一些意外行为。 By doing:通过做:

<div>{this.scrollbar()}</div>

You are rendering the result of the function call.您正在渲染 function 调用的结果。 The function has no return value though, so it will render undefined .虽然 function 没有返回值,所以它会呈现undefined Since it really doesn't need to be rendered, you can just call in inside the render function directly:因为它真的不需要渲染,你可以直接在渲染内部调用 function :

render() {
  this.scrollbar();

  return (
    <div>
      <div id="progressBar"></div>
      <div id="scrollPath"></div>
    </div>
  )
}

1) Move your scrollbar function outside render. 1)将滚动条 function 移到渲染之外。

2) remove () these braces while calling scrollbar inside render function. 2)在渲染function中调用滚动条时删除()这些大括号。

  scrollbar = () => {
   const progress = document.getElementById("progressBar");
   const totalHeight = document.body.scrollHeight - window.innerHeight;
   window.onscroll = function () {
     const progressHeight = (window.pageYOffset / totalHeight) * 100;
     progress.style.height = progressHeight + "%";
   };
};

render() {
return (
  <div>
    <div id="progressBar"> {this.scrollbar}</div>
    <div id="scrollPath"></div>

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

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