How to avoid bind or inline arrow functions inside render method

前端 未结 4 1401
暖寄归人
暖寄归人 2020-11-22 06:10

We should avoid method binding inside render because during re-rendering it will create the new methods instead of using the old one, that will affect the performance.

4条回答
  •  死守一世寂寞
    2020-11-22 06:49

    First: A simple solution will be to create a component for the content inside a map function and pass the values as props and when you call the function from the child component you can pass the value to the function passed down as props.

    Parent

    deleteTodo = (val) => {
        console.log(val)
    }
    todos.map(el => 
         
    
    )
    

    MyComponent

    class MyComponent extends React.Component {
        deleteTodo = () => {
            this.props.onClick(this.props.val);
        }
        render() {
           return 
    {this.props.val}
    } }

    Sample snippet

    class Parent extends React.Component {
         _deleteTodo = (val) => {
            console.log(val)
        }
        render() {
            var todos = ['a', 'b', 'c'];
            return (
               
    {todos.map(el => )}
    ) } } class MyComponent extends React.Component { _deleteTodo = () => { console.log('here'); this.props.onClick(this.props.val); } render() { return
    {this.props.val}
    } } ReactDOM.render(, document.getElementById('app'));
    
    
    

    EDIT:

    Second: The other approach to it would be to use memoize and return a function

    constructor() {
        super();
        this._deleteTodoListener = _.memoize(
                       this._deleteTodo, (element) => {
                            return element.hashCode();
                        }
                  )
    }
    
    _deleteTodo = (element) => {
       //delete handling here
    }
    

    and using it like

    todos.map(el => 
    {el}
    )

    P.S. However this is not a best solution and will still result in multiple functions being created but is still an improvement over the initial case.

    Third: However a more appropriate solution to this will be to add an attribute to the topmost div and get the value from event like

    _deleteTodo = (e) => {
         console.log(e.currentTarget.getAttribute('data-value'));
    
     }
    
     todos.map(el => 
    {el}
    )

    However, in this case the attributes are converted to string using toString method and hence and object will be converted to [Object Object] and and array like ["1" , "2", "3"] as "1, 2, 3"

提交回复
热议问题