• <legend id='yBF9a'><style id='yBF9a'><dir id='yBF9a'><q id='yBF9a'></q></dir></style></legend>

      <small id='yBF9a'></small><noframes id='yBF9a'>

    1. <i id='yBF9a'><tr id='yBF9a'><dt id='yBF9a'><q id='yBF9a'><span id='yBF9a'><b id='yBF9a'><form id='yBF9a'><ins id='yBF9a'></ins><ul id='yBF9a'></ul><sub id='yBF9a'></sub></form><legend id='yBF9a'></legend><bdo id='yBF9a'><pre id='yBF9a'><center id='yBF9a'></center></pre></bdo></b><th id='yBF9a'></th></span></q></dt></tr></i><div id='yBF9a'><tfoot id='yBF9a'></tfoot><dl id='yBF9a'><fieldset id='yBF9a'></fieldset></dl></div>
    2. <tfoot id='yBF9a'></tfoot>
      • <bdo id='yBF9a'></bdo><ul id='yBF9a'></ul>

      1. JSX 道具不应该使用 .bind() - 如何避免使用绑定?

        JSX props should not use .bind() - how to avoid using bind?(JSX 道具不应该使用 .bind() - 如何避免使用绑定?)

          <tfoot id='k6Ggk'></tfoot>
            <tbody id='k6Ggk'></tbody>
        1. <small id='k6Ggk'></small><noframes id='k6Ggk'>

            • <legend id='k6Ggk'><style id='k6Ggk'><dir id='k6Ggk'><q id='k6Ggk'></q></dir></style></legend>
                <bdo id='k6Ggk'></bdo><ul id='k6Ggk'></ul>
                • <i id='k6Ggk'><tr id='k6Ggk'><dt id='k6Ggk'><q id='k6Ggk'><span id='k6Ggk'><b id='k6Ggk'><form id='k6Ggk'><ins id='k6Ggk'></ins><ul id='k6Ggk'></ul><sub id='k6Ggk'></sub></form><legend id='k6Ggk'></legend><bdo id='k6Ggk'><pre id='k6Ggk'><center id='k6Ggk'></center></pre></bdo></b><th id='k6Ggk'></th></span></q></dt></tr></i><div id='k6Ggk'><tfoot id='k6Ggk'></tfoot><dl id='k6Ggk'><fieldset id='k6Ggk'></fieldset></dl></div>
                  本文介绍了JSX 道具不应该使用 .bind() - 如何避免使用绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个容器,我需要更改显示表单或显示成功页面的 UI 表单.

                  I have a container that I need to change the UI form showing the form or showing a success page.

                  容器有一个 state.showSuccess,我需要 MyFormModule 才能调用容器来改变状态.

                  The container has a state.showSuccess and I need the MyFormModule to be able to call the container to change the state.

                  以下代码有效,但我收到以下警告:

                  The below code works but I'm getting the following warning:

                  JSX 属性不应使用 .bind()

                  JSX props should not use .bind()

                  如何在不使用 .bind() 的情况下使其工作?

                  How can I get this to work without using .bind()?

                  ...
                  const myPage = class extends React.Component {
                    state = { showSuccess: false };
                    showSuccess() {
                     this.setState({
                        showSuccess: true,
                      });
                    }
                    render() {
                      const { showSuccess } = this.state;
                      if (showSuccess) {...}
                      ....
                      <MyFormModule showSuccess={this.showSuccess.bind(this)} />
                  

                  推荐答案

                  你应该先了解为什么这是一种不好的做法.

                  You should first understand WHY this is a bad practice.

                  这里的主要原因是 .bind 正在返回一个新的函数引用.
                  这将发生在每个 render 调用上,这可能会导致性能下降.

                  The main reason here, is that .bind is returning a new function reference.
                  This will happen on each render call, which may lead to a performance hit.

                  你有两个选择:

                  1. 使用构造函数绑定您的处理程序(这只会运行一次).

                  1. Use the constructor to bind your handlers (this will run only once).

                  constructor(props) {
                    super(props);
                    this.showSuccess = this.showSuccess.bind(this);
                  }
                  

                • 或使用 箭头函数创建您的处理程序 所以他们将使用this 的词法上下文,因此你不需要在 bind 他们所有(你需要一个 babel 插件):

                • Or create your handlers with arrow functions so they will use the lexical context for this, hence you won't need to bind them at all (you will need a babel plugin):

                  showSuccess = () => {
                    this.setState({
                      showSuccess: true,
                    });
                  }
                  

                • 您应该使用这种模式(如其他人建议的那样):

                  You should not use this pattern (as others suggested):

                  showSuccess={() => this.showSuccess()}
                  

                  因为这也会在每次渲染时创建一个新函数.
                  因此,您可以绕过警告,但您仍然在以不良实践设计编写代码.

                  Because this will as well create a new function on each render.
                  So you may bypass the warning but you are still writing your code in a bad practice design.

                  来自 ESLint 文档:

                  JSX 属性中的绑定调用或箭头函数将创建一个全新的在每个渲染上都起作用.这对性能不利,因为它将导致垃圾收集器被调用的方式比现在更多必要的.如果一个全新的,它也可能导致不必要的重新渲染函数作为道具传递给使用引用的组件对 prop 进行相等性检查以确定它是否应该更新.

                  A bind call or arrow function in a JSX prop will create a brand new function on every single render. This is bad for performance, as it will result in the garbage collector being invoked way more than is necessary. It may also cause unnecessary re-renders if a brand new function is passed as a prop to a component that uses reference equality check on the prop to determine if it should update.

                  这篇关于JSX 道具不应该使用 .bind() - 如何避免使用绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                  <i id='mTuOr'><tr id='mTuOr'><dt id='mTuOr'><q id='mTuOr'><span id='mTuOr'><b id='mTuOr'><form id='mTuOr'><ins id='mTuOr'></ins><ul id='mTuOr'></ul><sub id='mTuOr'></sub></form><legend id='mTuOr'></legend><bdo id='mTuOr'><pre id='mTuOr'><center id='mTuOr'></center></pre></bdo></b><th id='mTuOr'></th></span></q></dt></tr></i><div id='mTuOr'><tfoot id='mTuOr'></tfoot><dl id='mTuOr'><fieldset id='mTuOr'></fieldset></dl></div>

                          <tbody id='mTuOr'></tbody>
                          <tfoot id='mTuOr'></tfoot>

                          <small id='mTuOr'></small><noframes id='mTuOr'>

                        1. <legend id='mTuOr'><style id='mTuOr'><dir id='mTuOr'><q id='mTuOr'></q></dir></style></legend>
                            <bdo id='mTuOr'></bdo><ul id='mTuOr'></ul>