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

2023-09-30前端开发问题
25

本文介绍了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() - 如何避免使用绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

    相关推荐

    js删除数组中指定元素的5种方法
    在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
    2024-11-22 前端开发问题
    182

    JavaScript小数运算出现多位的解决办法
    在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
    2024-10-18 前端开发问题
    301

    JavaScript(js)文件字符串中丢失"\"斜线的解决方法
    问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
    2024-10-17 前端开发问题
    437

    layui中table列表 增加属性 edit="date",不生效怎么办?
    如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
    2024-06-11 前端开发问题
    455

    Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
    Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
    2024-04-20 前端开发问题
    5

    “数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
    quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
    2024-04-20 前端开发问题
    5