如何避免在带有组件的React / Redux容器中进行绑定(ES6类)

迈克尔·普拉霍夫(Michael Plakhov)

我有一个简单的react / redux应用程序,其中包含两个目标自动选择组件的搜索表单容器:

class QuoteBox extends Component {
    onSelectSuggest(target, destination) {
        this.props.setDestination(destination, target);
    }

    render() {
        return (
            <Destination
                    direction="From"
                    key="dest-from"
                    enterText={this.props.enterText.bind(this)}
                    onSelectSuggest={this.onSelectSuggest.bind(this, "origin")}
                    dataSource={this.props.originSuggests} />

            <Destination
                    direction="To"
                    key="dest-to"
                    enterText={this.props.enterText.bind(this)}
                    onSelectSuggest={this.onSelectSuggest.bind(this, "destination")}
                    dataSource={this.props.destinationSuggests} />
        )
    }
}

function mapStateToProps(state) {
    return {
        originSuggests: state.originSuggests,
        destinationSuggests: state.destinationSuggests,
        origin: state.origin,
        destination: state.destination
    }
}

function mapDispatchToProps(dispatch) {
    return bindActionCreators({
        enterText: enterText,
        setDestination: setDestination
    }, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps )(QuoteBox)

而Destination是一个简单的组件,例如

export default class Destination extends Component {
    render() {
        return (
            <AutoComplete
                    onUpdateInput = { _.debounce((term) => {
                        this.props.enterText(term);
                        this.setState({
                            searchText: term
                        })
                    }, 300) }
                    onNewRequest = {(x) =>{this.props.onSelectSuggest(x)}}
                    dataSource={this.props.dataSource} />
        )
    }
}

它们只是在某些事件上调用通过props传递的函数。

有自动绑定功能,但是在ES6类中不可用:https ://medium.com/@goatslacker/react-0-13-x-and-autobinding-b4906189425d#.nkv1cn32v我看到了许多替代方法,例如http:// www .ian-thomas.net / autobinding-react-and-es6-classes /或第三方库。

在将方法从容器传递到简单组件时,如何避免此bind(this)的现代方法是什么?

约书亚·科莫(Joshua Comeau)

有很多现代方法,但是它们都围绕着相同的想法:)

您可以使用箭头功能:

<Destination
  onSelectSuggest={destination => (
    this.props.setDestination(destination, 'origin')
  )}
/>

(我喜欢这一点,因为我们放弃了参数翻转onSelectSuggest方法!)

对于更简单的绑定,可以使用ESNext绑定运算符(::):

<Destination
  enterText={::this.enterText}
/>

(显然,那个需要Babel)


编辑2017年9月27日:“ bind”运算符似乎没有纳入规范。更现代的方法是使用属性初始值设定项语法(也是即将推出的JS功能,但我相信将其纳入语言的可能性更大):

class SomeComponent extends Component {
  enterText = () => {
    // body here
  }

  render() {
    return <Destination enterText={this.enterText} />
  }
}

最后,最有效的方法是在构造函数中进行绑定。

class QuoteBox extends Component {
  constructor(props) {
    super(props);
    this.enterText = this.props.enterText.bind(this);
  }
  render() {
    return (
      <Destination
        enterText={this.enterText}
      />
    )
  }
}

之所以更有效,是因为绑定非常慢,在您的示例中,绑定是在每个渲染上完成的。最好在初始化后再执行一次。

就是说,这两种方式都可能无关紧要在性能成为问题之前,不要过分担心。React速度很快,除非您要处理非常复杂的组件树,否则差异可能微不足道。

同样,在AutoComplete中,应在构造函数中绑定去抖动的函数:

class Destination extends Component {
  constructor(props) {
    super(props);
    this.updateInput = _.debounce(this.updateInput, 300)

  updateInput(term) {
    this.props.enterText(term); 
    this.setState({
      searchText: term
    }) 
  }  

  render() {
    return (
      <AutoComplete
        onUpdateInput={this.updateInput}
        onNewRequest={this.props.onSelectSuggest}
      />
    )
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Ember CLI中使用ES6语法对Ember类进行子类化

来自分类Dev

如何枚举ES6类方法

来自分类Dev

导入组件以在ES6中进行服务器端渲染

来自分类Dev

带有ES6类的AngularJS 1.4,为空

来自分类Dev

如何将具有成员变量的React组件重构为es6类

来自分类Dev

如何使用ES6在AngularJs中进行依赖注入?

来自分类Dev

剔除组件未绑定视图模型(ES6)

来自分类Dev

React-Redux:如何将其绑定到es6中的父容器?

来自分类Dev

如何处理React嵌套组件循环依赖关系?(使用es6类)

来自分类Dev

Redux / React教程没有ES6?

来自分类Dev

使用带有redux的react-native-router-flux,如何更新视图组件中的状态?

来自分类Dev

如何在React组件和es6类中使用继承

来自分类Dev

如何避免在带有组件的React / Redux容器中进行绑定(ES6类)

来自分类Dev

将React组件从函数重构为ES6类

来自分类Dev

如何为ES6类React组件创建Chai / Mocha单元测试?

来自分类Dev

如何避免React组件自动绑定“ this”?

来自分类Dev

react-i18next设置失败,带有react-redux组件

来自分类Dev

如何在带有thunk的react-redux挂钩中进行异步调用?

来自分类Dev

如何避免在使用ES6的React中使用绑定?

来自分类Dev

带有Angular的React样式控制的组件(绑定输入)

来自分类Dev

为什么在具有getters / setter方法的Es6类中无法进行记忆化?

来自分类Dev

专用路由功能代码将转换为React-Redux的ES6类版本

来自分类Dev

如何使用带有react-redux的react-router-dom v6.0.0渲染不同的组件

来自分类Dev

如何避免在VBA中进行引用(早期绑定与后期绑定)

来自分类Dev

如何在Ember CLI中使用ES6语法对Ember类进行子类化

来自分类Dev

带有ES6类的AngularJS 1.4,为空

来自分类Dev

我如何在这个 ES6 JS 代码中进行 Promise 链接

来自分类Dev

带有 ES6 类的 JavaScript 模式

来自分类Dev

如何为 ES6 类创建“字段”(React 示例)

Related 相关文章

  1. 1

    如何在Ember CLI中使用ES6语法对Ember类进行子类化

  2. 2

    如何枚举ES6类方法

  3. 3

    导入组件以在ES6中进行服务器端渲染

  4. 4

    带有ES6类的AngularJS 1.4,为空

  5. 5

    如何将具有成员变量的React组件重构为es6类

  6. 6

    如何使用ES6在AngularJs中进行依赖注入?

  7. 7

    剔除组件未绑定视图模型(ES6)

  8. 8

    React-Redux:如何将其绑定到es6中的父容器?

  9. 9

    如何处理React嵌套组件循环依赖关系?(使用es6类)

  10. 10

    Redux / React教程没有ES6?

  11. 11

    使用带有redux的react-native-router-flux,如何更新视图组件中的状态?

  12. 12

    如何在React组件和es6类中使用继承

  13. 13

    如何避免在带有组件的React / Redux容器中进行绑定(ES6类)

  14. 14

    将React组件从函数重构为ES6类

  15. 15

    如何为ES6类React组件创建Chai / Mocha单元测试?

  16. 16

    如何避免React组件自动绑定“ this”?

  17. 17

    react-i18next设置失败,带有react-redux组件

  18. 18

    如何在带有thunk的react-redux挂钩中进行异步调用?

  19. 19

    如何避免在使用ES6的React中使用绑定?

  20. 20

    带有Angular的React样式控制的组件(绑定输入)

  21. 21

    为什么在具有getters / setter方法的Es6类中无法进行记忆化?

  22. 22

    专用路由功能代码将转换为React-Redux的ES6类版本

  23. 23

    如何使用带有react-redux的react-router-dom v6.0.0渲染不同的组件

  24. 24

    如何避免在VBA中进行引用(早期绑定与后期绑定)

  25. 25

    如何在Ember CLI中使用ES6语法对Ember类进行子类化

  26. 26

    带有ES6类的AngularJS 1.4,为空

  27. 27

    我如何在这个 ES6 JS 代码中进行 Promise 链接

  28. 28

    带有 ES6 类的 JavaScript 模式

  29. 29

    如何为 ES6 类创建“字段”(React 示例)

热门标签

归档