How to stop subscribing store updates?
I move user from component1 to component2 by this.props.history.push. When I am in component2 it looks like component1 is still mounted.
I tried everything as in similar problems with unsubscribe store.
import React, {Component} from 'react';
import axios from 'axios';
import {Animate} from 'react-animate-mount';
import {translateTexts} from "../App";
import store from "../../store";
class Component1 extends Component {
constructor(props) {
super(props);
this.state = {
texts: {
computing: null
},
};
}
componentWillUnmount() {
return store.subscribe(() => {
console.log('unmount');
})
}
componentWillMount() {
store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
axios.post(`/api/`)
.then((res) => {
this.setState({show: false});
setTimeout(() => {
window.location.href = '/storage/'+res.data.id
}, 600);
})
.catch((err) => {
this.props.history.push({
pathname: '/error/'+err.response.status,
state: { code: err.response.status }});
});
});
render() {
return (
<div className="box">
Something
</div>
);
}
}
export default Component1;
import React, { Component } from 'react';
import store from "../../store";
import {translateTexts} from "../App";
class Component2 extends Component {
constructor(props) {
super(props);
this.state = {
code : null,
texts: {
msg: null
}
};
}
componentWillMount() {
this.setState( {
code: this.props.location.state.code,
});
store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
}
render() {
return (
<div>
Code: {this.state.code}
<br></br>
Message: <strong>{this.state.texts.msg}</strong>
</div>
);
}
}
export default Component2;
After componentWillUnmount I would to stop subscribing store events in component, because now when my store updates I see log unmount in my console, so Component1 is somehow mounted.
Thanks for any suggestions.
javascript reactjs redux react-redux store
add a comment |
I move user from component1 to component2 by this.props.history.push. When I am in component2 it looks like component1 is still mounted.
I tried everything as in similar problems with unsubscribe store.
import React, {Component} from 'react';
import axios from 'axios';
import {Animate} from 'react-animate-mount';
import {translateTexts} from "../App";
import store from "../../store";
class Component1 extends Component {
constructor(props) {
super(props);
this.state = {
texts: {
computing: null
},
};
}
componentWillUnmount() {
return store.subscribe(() => {
console.log('unmount');
})
}
componentWillMount() {
store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
axios.post(`/api/`)
.then((res) => {
this.setState({show: false});
setTimeout(() => {
window.location.href = '/storage/'+res.data.id
}, 600);
})
.catch((err) => {
this.props.history.push({
pathname: '/error/'+err.response.status,
state: { code: err.response.status }});
});
});
render() {
return (
<div className="box">
Something
</div>
);
}
}
export default Component1;
import React, { Component } from 'react';
import store from "../../store";
import {translateTexts} from "../App";
class Component2 extends Component {
constructor(props) {
super(props);
this.state = {
code : null,
texts: {
msg: null
}
};
}
componentWillMount() {
this.setState( {
code: this.props.location.state.code,
});
store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
}
render() {
return (
<div>
Code: {this.state.code}
<br></br>
Message: <strong>{this.state.texts.msg}</strong>
</div>
);
}
}
export default Component2;
After componentWillUnmount I would to stop subscribing store events in component, because now when my store updates I see log unmount in my console, so Component1 is somehow mounted.
Thanks for any suggestions.
javascript reactjs redux react-redux store
A good practice is to keep your subscription open until the component unmounts. You can usecomponentWillUnmountto unsubscribe. You could also subscribe in the constructor, but I would suggest you don't do this as that's not what constructors are for.
– Baruch
Jan 18 at 16:04
add a comment |
I move user from component1 to component2 by this.props.history.push. When I am in component2 it looks like component1 is still mounted.
I tried everything as in similar problems with unsubscribe store.
import React, {Component} from 'react';
import axios from 'axios';
import {Animate} from 'react-animate-mount';
import {translateTexts} from "../App";
import store from "../../store";
class Component1 extends Component {
constructor(props) {
super(props);
this.state = {
texts: {
computing: null
},
};
}
componentWillUnmount() {
return store.subscribe(() => {
console.log('unmount');
})
}
componentWillMount() {
store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
axios.post(`/api/`)
.then((res) => {
this.setState({show: false});
setTimeout(() => {
window.location.href = '/storage/'+res.data.id
}, 600);
})
.catch((err) => {
this.props.history.push({
pathname: '/error/'+err.response.status,
state: { code: err.response.status }});
});
});
render() {
return (
<div className="box">
Something
</div>
);
}
}
export default Component1;
import React, { Component } from 'react';
import store from "../../store";
import {translateTexts} from "../App";
class Component2 extends Component {
constructor(props) {
super(props);
this.state = {
code : null,
texts: {
msg: null
}
};
}
componentWillMount() {
this.setState( {
code: this.props.location.state.code,
});
store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
}
render() {
return (
<div>
Code: {this.state.code}
<br></br>
Message: <strong>{this.state.texts.msg}</strong>
</div>
);
}
}
export default Component2;
After componentWillUnmount I would to stop subscribing store events in component, because now when my store updates I see log unmount in my console, so Component1 is somehow mounted.
Thanks for any suggestions.
javascript reactjs redux react-redux store
I move user from component1 to component2 by this.props.history.push. When I am in component2 it looks like component1 is still mounted.
I tried everything as in similar problems with unsubscribe store.
import React, {Component} from 'react';
import axios from 'axios';
import {Animate} from 'react-animate-mount';
import {translateTexts} from "../App";
import store from "../../store";
class Component1 extends Component {
constructor(props) {
super(props);
this.state = {
texts: {
computing: null
},
};
}
componentWillUnmount() {
return store.subscribe(() => {
console.log('unmount');
})
}
componentWillMount() {
store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
axios.post(`/api/`)
.then((res) => {
this.setState({show: false});
setTimeout(() => {
window.location.href = '/storage/'+res.data.id
}, 600);
})
.catch((err) => {
this.props.history.push({
pathname: '/error/'+err.response.status,
state: { code: err.response.status }});
});
});
render() {
return (
<div className="box">
Something
</div>
);
}
}
export default Component1;
import React, { Component } from 'react';
import store from "../../store";
import {translateTexts} from "../App";
class Component2 extends Component {
constructor(props) {
super(props);
this.state = {
code : null,
texts: {
msg: null
}
};
}
componentWillMount() {
this.setState( {
code: this.props.location.state.code,
});
store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
}
render() {
return (
<div>
Code: {this.state.code}
<br></br>
Message: <strong>{this.state.texts.msg}</strong>
</div>
);
}
}
export default Component2;
After componentWillUnmount I would to stop subscribing store events in component, because now when my store updates I see log unmount in my console, so Component1 is somehow mounted.
Thanks for any suggestions.
javascript reactjs redux react-redux store
javascript reactjs redux react-redux store
asked Jan 18 at 15:59
bddddbdddd
317
317
A good practice is to keep your subscription open until the component unmounts. You can usecomponentWillUnmountto unsubscribe. You could also subscribe in the constructor, but I would suggest you don't do this as that's not what constructors are for.
– Baruch
Jan 18 at 16:04
add a comment |
A good practice is to keep your subscription open until the component unmounts. You can usecomponentWillUnmountto unsubscribe. You could also subscribe in the constructor, but I would suggest you don't do this as that's not what constructors are for.
– Baruch
Jan 18 at 16:04
A good practice is to keep your subscription open until the component unmounts. You can use
componentWillUnmount to unsubscribe. You could also subscribe in the constructor, but I would suggest you don't do this as that's not what constructors are for.– Baruch
Jan 18 at 16:04
A good practice is to keep your subscription open until the component unmounts. You can use
componentWillUnmount to unsubscribe. You could also subscribe in the constructor, but I would suggest you don't do this as that's not what constructors are for.– Baruch
Jan 18 at 16:04
add a comment |
2 Answers
2
active
oldest
votes
Store subscribe should return the unsubscribe function which you can call at componentWillUnmount to successfully unsubscribe from the store.
you can define and store it in state directly in componentWillMount:
this.setState({
unsubscribe: store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
});
and then call unsubscribe on componentWillUnmount
componentWillUnmount() {
this.state.unsubscribe()
}
Redux docs: https://redux.js.org/api/store#subscribe
New contributor
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Thank you... :-)
– bdddd
Jan 18 at 17:12
add a comment |
If you're using React and Redux together, you really shouldn't be interacting with the store directly in your component. Instead, you should be using the official React-Redux library, which handles all the subscription and store update process for you. See the docs page on Why Use React Redux? for more details.
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%2f54257462%2fhow-to-stop-subscribing-store-updates%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
Store subscribe should return the unsubscribe function which you can call at componentWillUnmount to successfully unsubscribe from the store.
you can define and store it in state directly in componentWillMount:
this.setState({
unsubscribe: store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
});
and then call unsubscribe on componentWillUnmount
componentWillUnmount() {
this.state.unsubscribe()
}
Redux docs: https://redux.js.org/api/store#subscribe
New contributor
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Thank you... :-)
– bdddd
Jan 18 at 17:12
add a comment |
Store subscribe should return the unsubscribe function which you can call at componentWillUnmount to successfully unsubscribe from the store.
you can define and store it in state directly in componentWillMount:
this.setState({
unsubscribe: store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
});
and then call unsubscribe on componentWillUnmount
componentWillUnmount() {
this.state.unsubscribe()
}
Redux docs: https://redux.js.org/api/store#subscribe
New contributor
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Thank you... :-)
– bdddd
Jan 18 at 17:12
add a comment |
Store subscribe should return the unsubscribe function which you can call at componentWillUnmount to successfully unsubscribe from the store.
you can define and store it in state directly in componentWillMount:
this.setState({
unsubscribe: store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
});
and then call unsubscribe on componentWillUnmount
componentWillUnmount() {
this.state.unsubscribe()
}
Redux docs: https://redux.js.org/api/store#subscribe
New contributor
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Store subscribe should return the unsubscribe function which you can call at componentWillUnmount to successfully unsubscribe from the store.
you can define and store it in state directly in componentWillMount:
this.setState({
unsubscribe: store.subscribe(() => {
translateTexts(this.state.texts),store.getState().translate.userLanguage).then((res) => {{
this.setState({texts: res.data});
});
});
});
and then call unsubscribe on componentWillUnmount
componentWillUnmount() {
this.state.unsubscribe()
}
Redux docs: https://redux.js.org/api/store#subscribe
New contributor
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Jan 18 at 16:16
New contributor
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Jan 18 at 16:10
Giorgos SiderisGiorgos Sideris
263
263
New contributor
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Giorgos Sideris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Thank you... :-)
– bdddd
Jan 18 at 17:12
add a comment |
1
Thank you... :-)
– bdddd
Jan 18 at 17:12
1
1
Thank you... :-)
– bdddd
Jan 18 at 17:12
Thank you... :-)
– bdddd
Jan 18 at 17:12
add a comment |
If you're using React and Redux together, you really shouldn't be interacting with the store directly in your component. Instead, you should be using the official React-Redux library, which handles all the subscription and store update process for you. See the docs page on Why Use React Redux? for more details.
add a comment |
If you're using React and Redux together, you really shouldn't be interacting with the store directly in your component. Instead, you should be using the official React-Redux library, which handles all the subscription and store update process for you. See the docs page on Why Use React Redux? for more details.
add a comment |
If you're using React and Redux together, you really shouldn't be interacting with the store directly in your component. Instead, you should be using the official React-Redux library, which handles all the subscription and store update process for you. See the docs page on Why Use React Redux? for more details.
If you're using React and Redux together, you really shouldn't be interacting with the store directly in your component. Instead, you should be using the official React-Redux library, which handles all the subscription and store update process for you. See the docs page on Why Use React Redux? for more details.
answered Jan 18 at 17:02
markeriksonmarkerikson
21.4k44467
21.4k44467
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%2f54257462%2fhow-to-stop-subscribing-store-updates%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
A good practice is to keep your subscription open until the component unmounts. You can use
componentWillUnmountto unsubscribe. You could also subscribe in the constructor, but I would suggest you don't do this as that's not what constructors are for.– Baruch
Jan 18 at 16:04