bindActionCreators, @connect in React-Redux and understanding the code










2















I was trying to comprehend this code on github



Here they programmer have used something like this



 constructor(props) 
super(props);
this.actions = bindActionCreators(Actions, this.props.dispatch);



Question 1: Here, this.actions = bindActionCreators(Actions, this.props.dispatch); doesn't make sense to me



Also, This isn't primary question but from where I learned React-redux, we used to connect component like this



export default connect(mapStateToProps, 
fetchCoin,
updateCrypto,
CurrencyRate,
CurrencyState
)(cryptoTicker);


In the above code Snippet the programmer have done something like this



@connect(state => 
return
score: state.game.get("score"),
result: state.game.get("result")
;
)


Question 2: ** Which again sort of looks alien to me (like I have few months of experience using js but this is the first time I am encountering **@) So can someone please explain me what is @ in general in JS?



And lastly he have declared two functions which are being called onclick after return statement of class



 _handleNewGame = () => 
this.actions.initGame();


/**
* Save the current game`s state.
*/
_handleSaveGame = () =>
this.actions.saveGame();



Now Since I am still relatively new to JS and react, I have two questions for this part



Question 3: Can we create a function inside a class after return and render? If yes then wouldn't it have been a good practise (in general) to create a function before render where all the other functions are declared?










share|improve this question






















  • It appears the github code you're referenced is a couple years old. I highly recommend following the example on the official Redux site (redux.js.org/basics/exampletodolist) to understand how the code works. For example, the official site uses non-decorator syntax which i find to be easier to understand than @connect.

    – Shawn Andrews
    Nov 15 '18 at 0:06











  • @ShawnAndrews even i use the same way (as shown in question) but I am just trying to comprehend his code. Either way, can you answer the other two part of the question?

    – iRohitBhatia
    Nov 15 '18 at 0:07
















2















I was trying to comprehend this code on github



Here they programmer have used something like this



 constructor(props) 
super(props);
this.actions = bindActionCreators(Actions, this.props.dispatch);



Question 1: Here, this.actions = bindActionCreators(Actions, this.props.dispatch); doesn't make sense to me



Also, This isn't primary question but from where I learned React-redux, we used to connect component like this



export default connect(mapStateToProps, 
fetchCoin,
updateCrypto,
CurrencyRate,
CurrencyState
)(cryptoTicker);


In the above code Snippet the programmer have done something like this



@connect(state => 
return
score: state.game.get("score"),
result: state.game.get("result")
;
)


Question 2: ** Which again sort of looks alien to me (like I have few months of experience using js but this is the first time I am encountering **@) So can someone please explain me what is @ in general in JS?



And lastly he have declared two functions which are being called onclick after return statement of class



 _handleNewGame = () => 
this.actions.initGame();


/**
* Save the current game`s state.
*/
_handleSaveGame = () =>
this.actions.saveGame();



Now Since I am still relatively new to JS and react, I have two questions for this part



Question 3: Can we create a function inside a class after return and render? If yes then wouldn't it have been a good practise (in general) to create a function before render where all the other functions are declared?










share|improve this question






















  • It appears the github code you're referenced is a couple years old. I highly recommend following the example on the official Redux site (redux.js.org/basics/exampletodolist) to understand how the code works. For example, the official site uses non-decorator syntax which i find to be easier to understand than @connect.

    – Shawn Andrews
    Nov 15 '18 at 0:06











  • @ShawnAndrews even i use the same way (as shown in question) but I am just trying to comprehend his code. Either way, can you answer the other two part of the question?

    – iRohitBhatia
    Nov 15 '18 at 0:07














2












2








2








I was trying to comprehend this code on github



Here they programmer have used something like this



 constructor(props) 
super(props);
this.actions = bindActionCreators(Actions, this.props.dispatch);



Question 1: Here, this.actions = bindActionCreators(Actions, this.props.dispatch); doesn't make sense to me



Also, This isn't primary question but from where I learned React-redux, we used to connect component like this



export default connect(mapStateToProps, 
fetchCoin,
updateCrypto,
CurrencyRate,
CurrencyState
)(cryptoTicker);


In the above code Snippet the programmer have done something like this



@connect(state => 
return
score: state.game.get("score"),
result: state.game.get("result")
;
)


Question 2: ** Which again sort of looks alien to me (like I have few months of experience using js but this is the first time I am encountering **@) So can someone please explain me what is @ in general in JS?



And lastly he have declared two functions which are being called onclick after return statement of class



 _handleNewGame = () => 
this.actions.initGame();


/**
* Save the current game`s state.
*/
_handleSaveGame = () =>
this.actions.saveGame();



Now Since I am still relatively new to JS and react, I have two questions for this part



Question 3: Can we create a function inside a class after return and render? If yes then wouldn't it have been a good practise (in general) to create a function before render where all the other functions are declared?










share|improve this question














I was trying to comprehend this code on github



Here they programmer have used something like this



 constructor(props) 
super(props);
this.actions = bindActionCreators(Actions, this.props.dispatch);



Question 1: Here, this.actions = bindActionCreators(Actions, this.props.dispatch); doesn't make sense to me



Also, This isn't primary question but from where I learned React-redux, we used to connect component like this



export default connect(mapStateToProps, 
fetchCoin,
updateCrypto,
CurrencyRate,
CurrencyState
)(cryptoTicker);


In the above code Snippet the programmer have done something like this



@connect(state => 
return
score: state.game.get("score"),
result: state.game.get("result")
;
)


Question 2: ** Which again sort of looks alien to me (like I have few months of experience using js but this is the first time I am encountering **@) So can someone please explain me what is @ in general in JS?



And lastly he have declared two functions which are being called onclick after return statement of class



 _handleNewGame = () => 
this.actions.initGame();


/**
* Save the current game`s state.
*/
_handleSaveGame = () =>
this.actions.saveGame();



Now Since I am still relatively new to JS and react, I have two questions for this part



Question 3: Can we create a function inside a class after return and render? If yes then wouldn't it have been a good practise (in general) to create a function before render where all the other functions are declared?







javascript reactjs redux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 23:50









iRohitBhatiaiRohitBhatia

1,314532




1,314532












  • It appears the github code you're referenced is a couple years old. I highly recommend following the example on the official Redux site (redux.js.org/basics/exampletodolist) to understand how the code works. For example, the official site uses non-decorator syntax which i find to be easier to understand than @connect.

    – Shawn Andrews
    Nov 15 '18 at 0:06











  • @ShawnAndrews even i use the same way (as shown in question) but I am just trying to comprehend his code. Either way, can you answer the other two part of the question?

    – iRohitBhatia
    Nov 15 '18 at 0:07


















  • It appears the github code you're referenced is a couple years old. I highly recommend following the example on the official Redux site (redux.js.org/basics/exampletodolist) to understand how the code works. For example, the official site uses non-decorator syntax which i find to be easier to understand than @connect.

    – Shawn Andrews
    Nov 15 '18 at 0:06











  • @ShawnAndrews even i use the same way (as shown in question) but I am just trying to comprehend his code. Either way, can you answer the other two part of the question?

    – iRohitBhatia
    Nov 15 '18 at 0:07

















It appears the github code you're referenced is a couple years old. I highly recommend following the example on the official Redux site (redux.js.org/basics/exampletodolist) to understand how the code works. For example, the official site uses non-decorator syntax which i find to be easier to understand than @connect.

– Shawn Andrews
Nov 15 '18 at 0:06





It appears the github code you're referenced is a couple years old. I highly recommend following the example on the official Redux site (redux.js.org/basics/exampletodolist) to understand how the code works. For example, the official site uses non-decorator syntax which i find to be easier to understand than @connect.

– Shawn Andrews
Nov 15 '18 at 0:06













@ShawnAndrews even i use the same way (as shown in question) but I am just trying to comprehend his code. Either way, can you answer the other two part of the question?

– iRohitBhatia
Nov 15 '18 at 0:07






@ShawnAndrews even i use the same way (as shown in question) but I am just trying to comprehend his code. Either way, can you answer the other two part of the question?

– iRohitBhatia
Nov 15 '18 at 0:07













1 Answer
1






active

oldest

votes


















1














For question #1 and #2, your '@connect' and 'connect(mapStateToProps, ...)(cryptoTicker)' syntax is equivalent. The @ indicates to the parser that we're using a decorator for our function, the connect(...) function, and outputs the same class, in this case Game, but with added functionality. This is called a HoC (Higher Order Components) design pattern and you can read more about it here https://medium.com/@mappmechanic/react-utility-higher-order-components-as-decorators-tc39-stage-2-9e9f3a17688a



For question #3, you are correct, it is better practice to leave the render function for the last function in the component.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310490%2fbindactioncreators-connect-in-react-redux-and-understanding-the-code%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    For question #1 and #2, your '@connect' and 'connect(mapStateToProps, ...)(cryptoTicker)' syntax is equivalent. The @ indicates to the parser that we're using a decorator for our function, the connect(...) function, and outputs the same class, in this case Game, but with added functionality. This is called a HoC (Higher Order Components) design pattern and you can read more about it here https://medium.com/@mappmechanic/react-utility-higher-order-components-as-decorators-tc39-stage-2-9e9f3a17688a



    For question #3, you are correct, it is better practice to leave the render function for the last function in the component.






    share|improve this answer





























      1














      For question #1 and #2, your '@connect' and 'connect(mapStateToProps, ...)(cryptoTicker)' syntax is equivalent. The @ indicates to the parser that we're using a decorator for our function, the connect(...) function, and outputs the same class, in this case Game, but with added functionality. This is called a HoC (Higher Order Components) design pattern and you can read more about it here https://medium.com/@mappmechanic/react-utility-higher-order-components-as-decorators-tc39-stage-2-9e9f3a17688a



      For question #3, you are correct, it is better practice to leave the render function for the last function in the component.






      share|improve this answer



























        1












        1








        1







        For question #1 and #2, your '@connect' and 'connect(mapStateToProps, ...)(cryptoTicker)' syntax is equivalent. The @ indicates to the parser that we're using a decorator for our function, the connect(...) function, and outputs the same class, in this case Game, but with added functionality. This is called a HoC (Higher Order Components) design pattern and you can read more about it here https://medium.com/@mappmechanic/react-utility-higher-order-components-as-decorators-tc39-stage-2-9e9f3a17688a



        For question #3, you are correct, it is better practice to leave the render function for the last function in the component.






        share|improve this answer















        For question #1 and #2, your '@connect' and 'connect(mapStateToProps, ...)(cryptoTicker)' syntax is equivalent. The @ indicates to the parser that we're using a decorator for our function, the connect(...) function, and outputs the same class, in this case Game, but with added functionality. This is called a HoC (Higher Order Components) design pattern and you can read more about it here https://medium.com/@mappmechanic/react-utility-higher-order-components-as-decorators-tc39-stage-2-9e9f3a17688a



        For question #3, you are correct, it is better practice to leave the render function for the last function in the component.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 18:30

























        answered Nov 15 '18 at 0:25









        Shawn AndrewsShawn Andrews

        975617




        975617





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310490%2fbindactioncreators-connect-in-react-redux-and-understanding-the-code%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto