javascript - What's the '@' (at symbol) in the Redux @connect decorator? -
i learning redux react , stumbled upon code. not sure if redux specific or not, have seen following code snippet in 1 of examples.
@connect((state) => { return { key: state.a.b }; }) while functionality of connect pretty straightforward, don't understand @ before connect. isn't javascript operator if not wrong.
can explain please , why used?
update:
it in fact part of react-redux used connects react component redux store.
the @ symbol in fact javascript expression currently proposed signify decorators:
decorators make possible annotate , modify classes , properties @ design time.
here's example of setting redux without , decorator:
without decorator
import react 'react'; import * actioncreators './actioncreators'; import { bindactioncreators } 'redux'; import { connect } 'react-redux'; function mapstatetoprops(state) { return { todos: state.todos }; } function mapdispatchtoprops(dispatch) { return { actions: bindactioncreators(actioncreators, dispatch) }; } class myapp extends react.component { // ...define main app here } export default connect(mapstatetoprops, mapdispatchtoprops)(myapp); using decorator
import react 'react'; import * actioncreators './actioncreators'; import { bindactioncreators } 'redux'; import { connect } '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 main app here } both examples above equivalent, it's matter of preference. also, decorator syntax isn't built javascript runtimes yet, , still experimental , subject change. if want use it, available using babel.
Comments
Post a Comment