Redux connected react app - how do I NOT render it until my side effect is complete?
React 16.3
Redux 3.7.2
In componentDidMount
I call an action that populates the props (mapDispatchToProps)
thru an side effect (AJAX).
My problem is that it reuses the old props from redux - render that first and then re-render once the update has arrived thru mapDispatchToProps
.
I don't want to have this child component remember it's state or props - I need it to be a blank slate each time.
The documentation states that the the component is destroyed on unmount but that doesn't seem to be the case since it's a difference in state when you go to this page or you are on the page and reload.
Because when you reload there is no data so the page HAS to wait for the sideeffect to complete.
Unfortunately I can't provide you with a code sample - but anyone that has been exposed to this weird behavior should recognize my description....
javascript reactjs redux react-redux
add a comment |
React 16.3
Redux 3.7.2
In componentDidMount
I call an action that populates the props (mapDispatchToProps)
thru an side effect (AJAX).
My problem is that it reuses the old props from redux - render that first and then re-render once the update has arrived thru mapDispatchToProps
.
I don't want to have this child component remember it's state or props - I need it to be a blank slate each time.
The documentation states that the the component is destroyed on unmount but that doesn't seem to be the case since it's a difference in state when you go to this page or you are on the page and reload.
Because when you reload there is no data so the page HAS to wait for the sideeffect to complete.
Unfortunately I can't provide you with a code sample - but anyone that has been exposed to this weird behavior should recognize my description....
javascript reactjs redux react-redux
well without any code reference, it is really hard to get an idea of your issue ? Can you try and share the minimum code possible ?
– Pranay Tripathi
Nov 25 '18 at 20:22
@PranayTripathi this problem is actually pretty easy to understand without a code example.
– Alex McMillan
Nov 25 '18 at 22:56
add a comment |
React 16.3
Redux 3.7.2
In componentDidMount
I call an action that populates the props (mapDispatchToProps)
thru an side effect (AJAX).
My problem is that it reuses the old props from redux - render that first and then re-render once the update has arrived thru mapDispatchToProps
.
I don't want to have this child component remember it's state or props - I need it to be a blank slate each time.
The documentation states that the the component is destroyed on unmount but that doesn't seem to be the case since it's a difference in state when you go to this page or you are on the page and reload.
Because when you reload there is no data so the page HAS to wait for the sideeffect to complete.
Unfortunately I can't provide you with a code sample - but anyone that has been exposed to this weird behavior should recognize my description....
javascript reactjs redux react-redux
React 16.3
Redux 3.7.2
In componentDidMount
I call an action that populates the props (mapDispatchToProps)
thru an side effect (AJAX).
My problem is that it reuses the old props from redux - render that first and then re-render once the update has arrived thru mapDispatchToProps
.
I don't want to have this child component remember it's state or props - I need it to be a blank slate each time.
The documentation states that the the component is destroyed on unmount but that doesn't seem to be the case since it's a difference in state when you go to this page or you are on the page and reload.
Because when you reload there is no data so the page HAS to wait for the sideeffect to complete.
Unfortunately I can't provide you with a code sample - but anyone that has been exposed to this weird behavior should recognize my description....
javascript reactjs redux react-redux
javascript reactjs redux react-redux
edited Nov 25 '18 at 20:19
Alex McMillan
8,03633565
8,03633565
asked Nov 25 '18 at 19:58
jBoivejBoive
437424
437424
well without any code reference, it is really hard to get an idea of your issue ? Can you try and share the minimum code possible ?
– Pranay Tripathi
Nov 25 '18 at 20:22
@PranayTripathi this problem is actually pretty easy to understand without a code example.
– Alex McMillan
Nov 25 '18 at 22:56
add a comment |
well without any code reference, it is really hard to get an idea of your issue ? Can you try and share the minimum code possible ?
– Pranay Tripathi
Nov 25 '18 at 20:22
@PranayTripathi this problem is actually pretty easy to understand without a code example.
– Alex McMillan
Nov 25 '18 at 22:56
well without any code reference, it is really hard to get an idea of your issue ? Can you try and share the minimum code possible ?
– Pranay Tripathi
Nov 25 '18 at 20:22
well without any code reference, it is really hard to get an idea of your issue ? Can you try and share the minimum code possible ?
– Pranay Tripathi
Nov 25 '18 at 20:22
@PranayTripathi this problem is actually pretty easy to understand without a code example.
– Alex McMillan
Nov 25 '18 at 22:56
@PranayTripathi this problem is actually pretty easy to understand without a code example.
– Alex McMillan
Nov 25 '18 at 22:56
add a comment |
1 Answer
1
active
oldest
votes
Your component is likely re-rendering with the "old" state because that's the state you've stored in redux, and are passing to the component.
What you could do is trigger a redux action in componentWillUnmount
which destroys that state.
EG (pseudo-pseudocode):
Reducer:
const initialState = { counter: 0 };
const myReducer = (state = initialState, action) => {
switch(action.type) {
case 'DO_SOMETHING':
return {
...state,
counter: state.counter + 1
};
case 'CLEAN_STATE':
return initialState;
}
}
Component:
class MyComponent extends React.Component {
render () {
return (
<div>
<span>{this.props.counter}</span>
<button onClick={triggerActionThatCausesDO_SOMETHING} />
</div>
);
}
componentWillUnmount () {
triggerActionThatCausesCLEAN_STATE();
}
}
Thanks! I'll try that.
– jBoive
Nov 25 '18 at 20:42
Yep - that was the solution! Thanks! =)
– jBoive
Nov 26 '18 at 10:48
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%2f53471356%2fredux-connected-react-app-how-do-i-not-render-it-until-my-side-effect-is-compl%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
Your component is likely re-rendering with the "old" state because that's the state you've stored in redux, and are passing to the component.
What you could do is trigger a redux action in componentWillUnmount
which destroys that state.
EG (pseudo-pseudocode):
Reducer:
const initialState = { counter: 0 };
const myReducer = (state = initialState, action) => {
switch(action.type) {
case 'DO_SOMETHING':
return {
...state,
counter: state.counter + 1
};
case 'CLEAN_STATE':
return initialState;
}
}
Component:
class MyComponent extends React.Component {
render () {
return (
<div>
<span>{this.props.counter}</span>
<button onClick={triggerActionThatCausesDO_SOMETHING} />
</div>
);
}
componentWillUnmount () {
triggerActionThatCausesCLEAN_STATE();
}
}
Thanks! I'll try that.
– jBoive
Nov 25 '18 at 20:42
Yep - that was the solution! Thanks! =)
– jBoive
Nov 26 '18 at 10:48
add a comment |
Your component is likely re-rendering with the "old" state because that's the state you've stored in redux, and are passing to the component.
What you could do is trigger a redux action in componentWillUnmount
which destroys that state.
EG (pseudo-pseudocode):
Reducer:
const initialState = { counter: 0 };
const myReducer = (state = initialState, action) => {
switch(action.type) {
case 'DO_SOMETHING':
return {
...state,
counter: state.counter + 1
};
case 'CLEAN_STATE':
return initialState;
}
}
Component:
class MyComponent extends React.Component {
render () {
return (
<div>
<span>{this.props.counter}</span>
<button onClick={triggerActionThatCausesDO_SOMETHING} />
</div>
);
}
componentWillUnmount () {
triggerActionThatCausesCLEAN_STATE();
}
}
Thanks! I'll try that.
– jBoive
Nov 25 '18 at 20:42
Yep - that was the solution! Thanks! =)
– jBoive
Nov 26 '18 at 10:48
add a comment |
Your component is likely re-rendering with the "old" state because that's the state you've stored in redux, and are passing to the component.
What you could do is trigger a redux action in componentWillUnmount
which destroys that state.
EG (pseudo-pseudocode):
Reducer:
const initialState = { counter: 0 };
const myReducer = (state = initialState, action) => {
switch(action.type) {
case 'DO_SOMETHING':
return {
...state,
counter: state.counter + 1
};
case 'CLEAN_STATE':
return initialState;
}
}
Component:
class MyComponent extends React.Component {
render () {
return (
<div>
<span>{this.props.counter}</span>
<button onClick={triggerActionThatCausesDO_SOMETHING} />
</div>
);
}
componentWillUnmount () {
triggerActionThatCausesCLEAN_STATE();
}
}
Your component is likely re-rendering with the "old" state because that's the state you've stored in redux, and are passing to the component.
What you could do is trigger a redux action in componentWillUnmount
which destroys that state.
EG (pseudo-pseudocode):
Reducer:
const initialState = { counter: 0 };
const myReducer = (state = initialState, action) => {
switch(action.type) {
case 'DO_SOMETHING':
return {
...state,
counter: state.counter + 1
};
case 'CLEAN_STATE':
return initialState;
}
}
Component:
class MyComponent extends React.Component {
render () {
return (
<div>
<span>{this.props.counter}</span>
<button onClick={triggerActionThatCausesDO_SOMETHING} />
</div>
);
}
componentWillUnmount () {
triggerActionThatCausesCLEAN_STATE();
}
}
edited Nov 25 '18 at 20:17
answered Nov 25 '18 at 20:12
Alex McMillanAlex McMillan
8,03633565
8,03633565
Thanks! I'll try that.
– jBoive
Nov 25 '18 at 20:42
Yep - that was the solution! Thanks! =)
– jBoive
Nov 26 '18 at 10:48
add a comment |
Thanks! I'll try that.
– jBoive
Nov 25 '18 at 20:42
Yep - that was the solution! Thanks! =)
– jBoive
Nov 26 '18 at 10:48
Thanks! I'll try that.
– jBoive
Nov 25 '18 at 20:42
Thanks! I'll try that.
– jBoive
Nov 25 '18 at 20:42
Yep - that was the solution! Thanks! =)
– jBoive
Nov 26 '18 at 10:48
Yep - that was the solution! Thanks! =)
– jBoive
Nov 26 '18 at 10:48
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%2f53471356%2fredux-connected-react-app-how-do-i-not-render-it-until-my-side-effect-is-compl%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
well without any code reference, it is really hard to get an idea of your issue ? Can you try and share the minimum code possible ?
– Pranay Tripathi
Nov 25 '18 at 20:22
@PranayTripathi this problem is actually pretty easy to understand without a code example.
– Alex McMillan
Nov 25 '18 at 22:56