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
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
add a comment |
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
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
add a comment |
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
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
reactjs redux react-hooks
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
thanks. Did some more research and came to this conclusion as well.
– bos570
Mar 11 at 16:46
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
thanks. Did some more research and came to this conclusion as well.
– bos570
Mar 11 at 16:46
add a comment |
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
thanks. Did some more research and came to this conclusion as well.
– bos570
Mar 11 at 16:46
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 10 at 7:53
jayarjojayarjo
6,676146398
6,676146398
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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