【问题标题】:Passing down onClick Functions: [ESLINT] JSX props should not use .bind() (react/jsx-no-bind)传递 onClick 函数:[ESLINT] JSX 道具不应使用 .bind() (react/jsx-no-bind)
【发布时间】:2023-03-12 21:55:01
【问题描述】:

我目前正在编写我的第一个 React 应用程序,我的 ESLINT 告诉我不应该在 JSX 道具上使用 .bind()。我知道这是因为 bind 正在创建新功能,因此会对性能产生负面影响。但是我不确定如何重构它以消除此错误。

如何在不使用绑定的情况下将我点击的元素传递给函数?

ForecastPage.jsx:

import React from 'react'
import api from '../shared/api'
import ForecastBox from './ForecastBox'
import DropdownSelector from './DropdownSelector'

const regions = [
  {
    name: 'Santa Cruz',
    id: '2958',
    spots:
    [
      { name: 'Steamer Lane', id: '4188' },
      { name: 'Four Mile', id: '5023' },
      { name: 'Waddell Creek', id: '5021' },
      { name: 'Mitchell\'s Cove', id: '5028' },
      { name: '26th Ave', id: '5030' },
    ],
  },
  {
    name: 'North Orange Country',
    id: '2143',
    spots:
    [
      { name: 'Newport', id: '1241' },
      { name: 'HB', id: '3421' },
    ],
  },
]

class ForecastPage extends React.Component {

  constructor(props) {
    super(props)
    this.state = {
      selectedRegion: null,
      selectedSpot: null,
      forecast: null,
    }

    this.regionSpotList = regions
    this.updateSpot = this.updateSpot.bind(this)
    this.updateRegion = this.updateRegion.bind(this)
  }

  updateRegion(region) {
    this.setState({
      selectedRegion: region,
      forecast: null,
    })

    api.fetchSpot(region.id)
    .then((forecast) => {
      this.setState({
        forecast,
      })
    })
  }

  updateSpot(spot) {
    this.setState({
      selectedSpot: spot,
      forecast: null,
    })

    api.fetchSpot(spot.id)
    .then((forecast) => {
      this.setState({
        forecast,
      })
    })
  }

  render() {
    return (
      <div>
        <div className="container-fluid row region-spot-select">
          <DropdownSelector
            options={this.regionSpotList}
            onSelect={this.updateRegion}
            title={this.state.selectedRegion == null ? 'Select Your Region' : this.state.selectedRegion.name}
            keyName={'region-selector'}
            id={'region-selector-dropdown'}
          />
          {this.state.selectedRegion != null &&
            <DropdownSelector
              options={this.state.selectedRegion.spots}
              onSelect={this.updateSpot}
              title={this.state.selectedSpot == null ||
              !this.state.selectedRegion.spots.includes(this.state.selectedSpot) ?
              'Select A Spot' :
              this.state.selectedSpot.name}
              keyName={'spot-selector'}
              id={'spot-selector-dropdown'}
            />
          }
        </div>
        <div>
          {!this.state.forecast ?
            <div>
              Select A Region
            </div>
          : <ForecastBox forecast={this.state.forecast} /> }
        </div>
      </div>
    )
  }
}

export default ForecastPage

DropdownSelector.jsx

// @flow

import React from 'react'
import PropTypes from 'prop-types'
import { DropdownButton, MenuItem } from 'react-bootstrap'

type Props = {
  options: Object,
  onSelect: Function,
  title: string,
  keyName: string,
  id: string,
}

const DropdownSelector = ({ title, options, keyName, id, onSelect }: Props) =>
  <div className="content">
    <div className="btn-group">
      <DropdownButton
        bsStyle={'primary'}
        title={title}
        key={keyName}
        id={id}
      >
        {options.map(element =>
          <MenuItem
            key={element.name}
            eventKey={element.name}
            // eslint-disable-next-line
            onClick={onSelect.bind(null, element)}
          >
            {element.name}
          </MenuItem>,
          )
        }
      </DropdownButton>
    </div>
  </div>


DropdownSelector.defaultProps = {
  id: null,
}

DropdownSelector.propTypes = {
  options: PropTypes.instanceOf(Object).isRequired,
  title: PropTypes.string.isRequired,
  onSelect: PropTypes.func.isRequired,
  keyName: PropTypes.string.isRequired,
  id: PropTypes.string,
}

export default DropdownSelector

【问题讨论】:

    标签: javascript reactjs eslint flowtype


    【解决方案1】:

    试试 Alex 的答案,但只是 onSelect,没有 'this'。

    【讨论】:

      【解决方案2】:

      你也可以使用箭头函数来完成同样的事情,比如

      onClick={(event) => this.props.onSelect(null, element)}
      

      但是,它具有您提到的相同的潜在负面性能问题。 React 文档在这方面非常出色,并列举了您的选择及其优缺点:https://facebook.github.io/react/docs/handling-events.html

      • 更新到this.props.onSelect,忘记了您将其作为道具传递,而不是在组件本身上定义它。如果你不使用事件对象,也许只是使用

        onClick={() => this.props.onSelect(null, element)}
        

      【讨论】:

      • 嗯,语法对我不起作用,(事件)已定义但从未使用过,this.onSelect 也未定义。
      • @Frenchy,更新了我的答案...忘记了您的 onSelect 处理程序是作为道具传递的
      • 感谢您的帮助!!我在循环更新状态时遇到了一些问题,但我以某种方式修复了它。我也只需要这样做:onClick={() =&gt; onSelect(element)}
      • 如果这回答了你的问题,别忘了接受。
      猜你喜欢
      • 2017-04-07
      • 1970-01-01
      • 2019-01-10
      • 2018-01-10
      • 2018-06-15
      • 2021-05-14
      • 1970-01-01
      • 2022-09-27
      • 2016-12-11
      相关资源
      最近更新 更多