237

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.

@connect((state) => {
  return {
    key: state.a.b
  };
})

While the functionality of connect is pretty straightforward, but I don't understand the @ before connect. It isn't even a JavaScript operator if I am not wrong.

Can someone explain please what is this and why is it used?

Update:

It is in fact a part of react-redux which is used to connects a React component to a Redux store.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Salman
  • 9,299
  • 6
  • 40
  • 73
  • 6
    I'm not familiar with Redux, but it looks like a decorator. https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841 – Lee Sep 18 '15 at 08:19
  • 4
    I love how in this new JavaScript world you are staring at the code half of the time and thinking "what part of the language syntax is this?" – MK. Nov 29 '16 at 05:11
  • 4
    Lol, I'm way deep into redux and stuff now. But back then I didn't know the decorator syntax has nothing to do with redux. Its just JavaScript. Glad to see this question is helping a lot of people like me. :) – Salman Nov 29 '16 at 12:16
  • 1
    Apparently the redux team discourages the use of connect as a decorator at the moment https://github.com/happypoulp/redux-tutorial/issues/87 – Syed Jafri Feb 26 '17 at 22:15

2 Answers2

385

The @ symbol is in fact a JavaScript expression currently proposed to signify decorators:

Decorators make it possible to annotate and modify classes and properties at design time.

Here's an example of setting up Redux without and with a decorator:

Without a decorator

import React from 'react';
import * as actionCreators from './actionCreators';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

function mapStateToProps(state) {
  return { todos: state.todos };
}

function mapDispatchToProps(dispatch) {
  return { actions: bindActionCreators(actionCreators, dispatch) };
}

class MyApp extends React.Component {
  // ...define your main app here
}

export default connect(mapStateToProps, mapDispatchToProps)(MyApp);

Using a decorator

import React from 'react';
import * as actionCreators from './actionCreators';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

function mapStateToProps(state) {
  return { todos: state.todos };
}

function mapDispatchToProps(dispatch) {
  return { actions: bindActionCreators(actionCreators, dispatch) };
}

@connect(mapStateToProps, mapDispatchToProps)
export default class MyApp extends React.Component {
  // ...define your main app here
}

Both examples above are equivalent, it's just a matter of preference. Also, the decorator syntax isn't built into any Javascript runtimes yet, and is still experimental and subject to change. If you want to use it, it is available using Babel.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Tanner Semerad
  • 12,472
  • 12
  • 40
  • 49
  • 2
    One can even be more terse with ES6 syntax. @connect( state => { return { todos: state.todos }; }, dispatch => { return {actions: bindActionCreators(actionCreators, dispatch)}; } ) – LessQuesar Oct 24 '16 at 17:46
  • 11
    If you really want to be terse you can use implicit returns in ES6. It depends on how explicit you want to be. `@connect(state => ({todos: state.todos}), dispatch => ({actions: bindActionCreators(actionCreators, dispatch)}))` – Tanner Semerad Oct 24 '16 at 18:22
  • 3
    How would you export the unconnected component for unit testing? – tim Apr 13 '17 at 09:30
  • Using decorator for redux with react-navigation can be problematic, current best-practice is to use the function not the decorator: https://github.com/react-community/react-navigation/issues/1180 – straya May 23 '17 at 03:02
  • The examples are really helpful – Subhadeep Banerjee Jun 27 '18 at 14:16
  • What does this mean `"at design time"`? Never heard this term *design time*. can you explain it? – vsync Sep 05 '18 at 19:07
  • Is this possible with functional components? – serg06 Mar 26 '22 at 15:36
51

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]
}))
Fareed Alnamrouti
  • 30,771
  • 4
  • 85
  • 76