How to use redux state with react hooksLoop inside React JSXProgrammatically navigate using react routerWhy use Redux over Facebook Flux?Redux - multiple stores, why not?How to get simple dispatch from this.props using connect w/ Redux?Pros/cons of using redux-saga with ES6 generators vs redux-thunk with ES2017 async/awaitHow to dispatch a Redux action with a timeout?How to show a loading indicator in React Redux app while fetching the data?React with Redux? What about the 'context' issue?Redux vs plain React

How to indicate a cut out for a product window

Creepy dinosaur pc game identification

Are the IPv6 address space and IPv4 address space completely disjoint?

Loading commands from file

Closed-form expression for certain product

GraphicsGrid with a Label for each Column and Row

Is it safe to use olive oil to clean the ear wax?

Creature in Shazam mid-credits scene?

How to implement a feedback to keep the DC gain at zero for this conceptual passive filter?

Why did the HMS Bounty go back to a time when whales are already rare?

Aragorn's "guise" in the Orthanc Stone

Which one is correct as adjective “protruding” or “protruded”?

Is a bound state a stationary state?

Biological Blimps: Propulsion

Why do compilers behave differently when static_cast(ing) a function to void*?

The screen of my macbook suddenly broken down how can I do to recover

If a character has darkvision, can they see through an area of nonmagical darkness filled with lightly obscuring gas?

Redundant comparison & "if" before assignment

What should you do when eye contact makes your subordinate uncomfortable?

Lowest total scrabble score

Open a doc from terminal, but not by its name

Calculating Wattage for Resistor in High Frequency Application?

Is this toilet slogan correct usage of the English language?

Is it improper etiquette to ask your opponent what his/her rating is before the game?



How to use redux state with react hooks


Loop inside React JSXProgrammatically navigate using react routerWhy use Redux over Facebook Flux?Redux - multiple stores, why not?How to get simple dispatch from this.props using connect w/ Redux?Pros/cons of using redux-saga with ES6 generators vs redux-thunk with ES2017 async/awaitHow to dispatch a Redux action with a timeout?How to show a loading indicator in React Redux app while fetching the data?React with Redux? What about the 'context' issue?Redux vs plain React













1















So we recently decided to start using hooks in our current react app. We are using redux for state management and my question was how does this work with hooks?



I've read some articles where people use the context api with hooks to create a state manager but I would like to keep using redux for now.



I know the react api has a useReducer method, can this be used to dispatch redux actions? I've been looking for a tutorial/example but can't seem to find any resources online for this. I may be headed down the wrong path but would like to know if I am. Thanks.










share|improve this question






















  • I came across this project a couple days ago, perhaps it might be of help to you? github.com/dai-shi/react-hooks-easy-redux

    – Keno Clayton
    Mar 8 at 5:10











  • @KenoClayton Thanks for sharing. I was trying to figure out if there was a way to do this with just new hooks api

    – bos570
    Mar 8 at 5:13






  • 1





    Redux is already managing the state of your component, using useReducer will manage redux calls of your component, this will double layer, and I really see no point on doing this.

    – Danyal Imran
    Mar 8 at 5:37






  • 2





    there's no official solution for this as yet as there are some performance issues to overcome. For now I'd say the best approach would be to continue using the Redux connect HOC with your function components the same way you are with your class components.

    – lecstor
    Mar 8 at 7:23
















1















So we recently decided to start using hooks in our current react app. We are using redux for state management and my question was how does this work with hooks?



I've read some articles where people use the context api with hooks to create a state manager but I would like to keep using redux for now.



I know the react api has a useReducer method, can this be used to dispatch redux actions? I've been looking for a tutorial/example but can't seem to find any resources online for this. I may be headed down the wrong path but would like to know if I am. Thanks.










share|improve this question






















  • I came across this project a couple days ago, perhaps it might be of help to you? github.com/dai-shi/react-hooks-easy-redux

    – Keno Clayton
    Mar 8 at 5:10











  • @KenoClayton Thanks for sharing. I was trying to figure out if there was a way to do this with just new hooks api

    – bos570
    Mar 8 at 5:13






  • 1





    Redux is already managing the state of your component, using useReducer will manage redux calls of your component, this will double layer, and I really see no point on doing this.

    – Danyal Imran
    Mar 8 at 5:37






  • 2





    there's no official solution for this as yet as there are some performance issues to overcome. For now I'd say the best approach would be to continue using the Redux connect HOC with your function components the same way you are with your class components.

    – lecstor
    Mar 8 at 7:23














1












1








1








So we recently decided to start using hooks in our current react app. We are using redux for state management and my question was how does this work with hooks?



I've read some articles where people use the context api with hooks to create a state manager but I would like to keep using redux for now.



I know the react api has a useReducer method, can this be used to dispatch redux actions? I've been looking for a tutorial/example but can't seem to find any resources online for this. I may be headed down the wrong path but would like to know if I am. Thanks.










share|improve this question














So we recently decided to start using hooks in our current react app. We are using redux for state management and my question was how does this work with hooks?



I've read some articles where people use the context api with hooks to create a state manager but I would like to keep using redux for now.



I know the react api has a useReducer method, can this be used to dispatch redux actions? I've been looking for a tutorial/example but can't seem to find any resources online for this. I may be headed down the wrong path but would like to know if I am. Thanks.







reactjs redux react-hooks






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 4:55









bos570bos570

423520




423520












  • I came across this project a couple days ago, perhaps it might be of help to you? github.com/dai-shi/react-hooks-easy-redux

    – Keno Clayton
    Mar 8 at 5:10











  • @KenoClayton Thanks for sharing. I was trying to figure out if there was a way to do this with just new hooks api

    – bos570
    Mar 8 at 5:13






  • 1





    Redux is already managing the state of your component, using useReducer will manage redux calls of your component, this will double layer, and I really see no point on doing this.

    – Danyal Imran
    Mar 8 at 5:37






  • 2





    there's no official solution for this as yet as there are some performance issues to overcome. For now I'd say the best approach would be to continue using the Redux connect HOC with your function components the same way you are with your class components.

    – lecstor
    Mar 8 at 7:23


















  • I came across this project a couple days ago, perhaps it might be of help to you? github.com/dai-shi/react-hooks-easy-redux

    – Keno Clayton
    Mar 8 at 5:10











  • @KenoClayton Thanks for sharing. I was trying to figure out if there was a way to do this with just new hooks api

    – bos570
    Mar 8 at 5:13






  • 1





    Redux is already managing the state of your component, using useReducer will manage redux calls of your component, this will double layer, and I really see no point on doing this.

    – Danyal Imran
    Mar 8 at 5:37






  • 2





    there's no official solution for this as yet as there are some performance issues to overcome. For now I'd say the best approach would be to continue using the Redux connect HOC with your function components the same way you are with your class components.

    – lecstor
    Mar 8 at 7:23

















I came across this project a couple days ago, perhaps it might be of help to you? github.com/dai-shi/react-hooks-easy-redux

– Keno Clayton
Mar 8 at 5:10





I came across this project a couple days ago, perhaps it might be of help to you? github.com/dai-shi/react-hooks-easy-redux

– Keno Clayton
Mar 8 at 5:10













@KenoClayton Thanks for sharing. I was trying to figure out if there was a way to do this with just new hooks api

– bos570
Mar 8 at 5:13





@KenoClayton Thanks for sharing. I was trying to figure out if there was a way to do this with just new hooks api

– bos570
Mar 8 at 5:13




1




1





Redux is already managing the state of your component, using useReducer will manage redux calls of your component, this will double layer, and I really see no point on doing this.

– Danyal Imran
Mar 8 at 5:37





Redux is already managing the state of your component, using useReducer will manage redux calls of your component, this will double layer, and I really see no point on doing this.

– Danyal Imran
Mar 8 at 5:37




2




2





there's no official solution for this as yet as there are some performance issues to overcome. For now I'd say the best approach would be to continue using the Redux connect HOC with your function components the same way you are with your class components.

– lecstor
Mar 8 at 7:23






there's no official solution for this as yet as there are some performance issues to overcome. For now I'd say the best approach would be to continue using the Redux connect HOC with your function components the same way you are with your class components.

– lecstor
Mar 8 at 7:23













2 Answers
2






active

oldest

votes


















0














Nothing changes with hooks when using Redux, Redux Higher Order Component has nothing to do with Hooks. useReducer isn't meant for dispatching Redux actions but for updating the component internal state the same way Redux does.



So when you use useReducer you will dispatch actions, update the state with a reducer etc. but not the Redux state! Instead, you're doing that with the component state.



A component that consumes useReducer is a standard component with an internal state (to manage input states or whatever you want) wrapped, as usual before the hooks born, in a Redux's connect HOC.



If it could be helpful you can clarify your ideas with this post






share|improve this answer























  • thanks. Did some more research and came to this conclusion as well.

    – bos570
    Mar 11 at 16:46


















1














I'm not a heavy user of the Redux (I prefer MobX), so I might be overlooking certain subtle aspects. But from what I see it's pretty straightforward and React docs on hooks provide very nice example:



const initialState = count: 0;

function reducer(state, action)
switch (action.type)
case 'increment':
return count: state.count + 1;
case 'decrement':
return count: state.count - 1;
default:
throw new Error();



function Counter(initialState)
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
Count: state.count
<button onClick=() => dispatch(type: 'increment')>+</button>
<button onClick=() => dispatch(type: 'decrement')>-</button>
</>
);



Instead of reducer here, you can simply use one of the related reducers from your existing state management code base. Simply import it into the component file and pass it to the useReducer as the first argument. The state that you will return from there will become a new state for the component and it will auto-rerender itself with it. Obviously you will need to disconnect such component from Redux (if it is connected). Otherwise as it was mentioned in one of the comments you will end up with redundant state management logic.



However on your place I wouldn't rewrite it for hooks without any utter necessity.






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%2f55056945%2fhow-to-use-redux-state-with-react-hooks%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Nothing changes with hooks when using Redux, Redux Higher Order Component has nothing to do with Hooks. useReducer isn't meant for dispatching Redux actions but for updating the component internal state the same way Redux does.



    So when you use useReducer you will dispatch actions, update the state with a reducer etc. but not the Redux state! Instead, you're doing that with the component state.



    A component that consumes useReducer is a standard component with an internal state (to manage input states or whatever you want) wrapped, as usual before the hooks born, in a Redux's connect HOC.



    If it could be helpful you can clarify your ideas with this post






    share|improve this answer























    • thanks. Did some more research and came to this conclusion as well.

      – bos570
      Mar 11 at 16:46















    0














    Nothing changes with hooks when using Redux, Redux Higher Order Component has nothing to do with Hooks. useReducer isn't meant for dispatching Redux actions but for updating the component internal state the same way Redux does.



    So when you use useReducer you will dispatch actions, update the state with a reducer etc. but not the Redux state! Instead, you're doing that with the component state.



    A component that consumes useReducer is a standard component with an internal state (to manage input states or whatever you want) wrapped, as usual before the hooks born, in a Redux's connect HOC.



    If it could be helpful you can clarify your ideas with this post






    share|improve this answer























    • thanks. Did some more research and came to this conclusion as well.

      – bos570
      Mar 11 at 16:46













    0












    0








    0







    Nothing changes with hooks when using Redux, Redux Higher Order Component has nothing to do with Hooks. useReducer isn't meant for dispatching Redux actions but for updating the component internal state the same way Redux does.



    So when you use useReducer you will dispatch actions, update the state with a reducer etc. but not the Redux state! Instead, you're doing that with the component state.



    A component that consumes useReducer is a standard component with an internal state (to manage input states or whatever you want) wrapped, as usual before the hooks born, in a Redux's connect HOC.



    If it could be helpful you can clarify your ideas with this post






    share|improve this answer













    Nothing changes with hooks when using Redux, Redux Higher Order Component has nothing to do with Hooks. useReducer isn't meant for dispatching Redux actions but for updating the component internal state the same way Redux does.



    So when you use useReducer you will dispatch actions, update the state with a reducer etc. but not the Redux state! Instead, you're doing that with the component state.



    A component that consumes useReducer is a standard component with an internal state (to manage input states or whatever you want) wrapped, as usual before the hooks born, in a Redux's connect HOC.



    If it could be helpful you can clarify your ideas with this post







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 11 at 6:33









    NoriSteNoriSte

    1,09388




    1,09388












    • thanks. Did some more research and came to this conclusion as well.

      – bos570
      Mar 11 at 16:46

















    • thanks. Did some more research and came to this conclusion as well.

      – bos570
      Mar 11 at 16:46
















    thanks. Did some more research and came to this conclusion as well.

    – bos570
    Mar 11 at 16:46





    thanks. Did some more research and came to this conclusion as well.

    – bos570
    Mar 11 at 16:46













    1














    I'm not a heavy user of the Redux (I prefer MobX), so I might be overlooking certain subtle aspects. But from what I see it's pretty straightforward and React docs on hooks provide very nice example:



    const initialState = count: 0;

    function reducer(state, action)
    switch (action.type)
    case 'increment':
    return count: state.count + 1;
    case 'decrement':
    return count: state.count - 1;
    default:
    throw new Error();



    function Counter(initialState)
    const [state, dispatch] = useReducer(reducer, initialState);
    return (
    <>
    Count: state.count
    <button onClick=() => dispatch(type: 'increment')>+</button>
    <button onClick=() => dispatch(type: 'decrement')>-</button>
    </>
    );



    Instead of reducer here, you can simply use one of the related reducers from your existing state management code base. Simply import it into the component file and pass it to the useReducer as the first argument. The state that you will return from there will become a new state for the component and it will auto-rerender itself with it. Obviously you will need to disconnect such component from Redux (if it is connected). Otherwise as it was mentioned in one of the comments you will end up with redundant state management logic.



    However on your place I wouldn't rewrite it for hooks without any utter necessity.






    share|improve this answer



























      1














      I'm not a heavy user of the Redux (I prefer MobX), so I might be overlooking certain subtle aspects. But from what I see it's pretty straightforward and React docs on hooks provide very nice example:



      const initialState = count: 0;

      function reducer(state, action)
      switch (action.type)
      case 'increment':
      return count: state.count + 1;
      case 'decrement':
      return count: state.count - 1;
      default:
      throw new Error();



      function Counter(initialState)
      const [state, dispatch] = useReducer(reducer, initialState);
      return (
      <>
      Count: state.count
      <button onClick=() => dispatch(type: 'increment')>+</button>
      <button onClick=() => dispatch(type: 'decrement')>-</button>
      </>
      );



      Instead of reducer here, you can simply use one of the related reducers from your existing state management code base. Simply import it into the component file and pass it to the useReducer as the first argument. The state that you will return from there will become a new state for the component and it will auto-rerender itself with it. Obviously you will need to disconnect such component from Redux (if it is connected). Otherwise as it was mentioned in one of the comments you will end up with redundant state management logic.



      However on your place I wouldn't rewrite it for hooks without any utter necessity.






      share|improve this answer

























        1












        1








        1







        I'm not a heavy user of the Redux (I prefer MobX), so I might be overlooking certain subtle aspects. But from what I see it's pretty straightforward and React docs on hooks provide very nice example:



        const initialState = count: 0;

        function reducer(state, action)
        switch (action.type)
        case 'increment':
        return count: state.count + 1;
        case 'decrement':
        return count: state.count - 1;
        default:
        throw new Error();



        function Counter(initialState)
        const [state, dispatch] = useReducer(reducer, initialState);
        return (
        <>
        Count: state.count
        <button onClick=() => dispatch(type: 'increment')>+</button>
        <button onClick=() => dispatch(type: 'decrement')>-</button>
        </>
        );



        Instead of reducer here, you can simply use one of the related reducers from your existing state management code base. Simply import it into the component file and pass it to the useReducer as the first argument. The state that you will return from there will become a new state for the component and it will auto-rerender itself with it. Obviously you will need to disconnect such component from Redux (if it is connected). Otherwise as it was mentioned in one of the comments you will end up with redundant state management logic.



        However on your place I wouldn't rewrite it for hooks without any utter necessity.






        share|improve this answer













        I'm not a heavy user of the Redux (I prefer MobX), so I might be overlooking certain subtle aspects. But from what I see it's pretty straightforward and React docs on hooks provide very nice example:



        const initialState = count: 0;

        function reducer(state, action)
        switch (action.type)
        case 'increment':
        return count: state.count + 1;
        case 'decrement':
        return count: state.count - 1;
        default:
        throw new Error();



        function Counter(initialState)
        const [state, dispatch] = useReducer(reducer, initialState);
        return (
        <>
        Count: state.count
        <button onClick=() => dispatch(type: 'increment')>+</button>
        <button onClick=() => dispatch(type: 'decrement')>-</button>
        </>
        );



        Instead of reducer here, you can simply use one of the related reducers from your existing state management code base. Simply import it into the component file and pass it to the useReducer as the first argument. The state that you will return from there will become a new state for the component and it will auto-rerender itself with it. Obviously you will need to disconnect such component from Redux (if it is connected). Otherwise as it was mentioned in one of the comments you will end up with redundant state management logic.



        However on your place I wouldn't rewrite it for hooks without any utter necessity.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 10 at 7:53









        jayarjojayarjo

        6,676146398




        6,676146398



























            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%2f55056945%2fhow-to-use-redux-state-with-react-hooks%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







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme