What's the '@' (at symbol) in the Redux @connect decorator?

前端 未结 2 1549
渐次进展
渐次进展 2020-11-29 15:00

I am learning Redux with React and stumbled upon this code. I am not sure if it is Redux specific or not, but I have seen the following code snippet in one of the examples.<

2条回答
  •  忘掉有多难
    2020-11-29 15:14

    Very Important!

    These props are called state props and they are different from normal props, any change to your component state props will trigger the component render method again and again even if you don't use these props so for Performance Reasons try to bind to your component only the state props that you need inside your component and if you use a sub props only bind these props.

    example: lets say inside your component you only need two props:

    1. the last message
    2. the user name

    don't do this

    @connect(state => ({ 
       user: state.user,
       messages: state.messages
    }))
    

    do this

    @connect(state => ({ 
       user_name: state.user.name,
       last_message: state.messages[state.messages.length-1]
    }))
    

提交回复
热议问题