Waleed Kasem
Waleed Kasem

Reputation: 145

react unmouting when navigate away

i have this react component which works so will when i render it

var Postlist = React.createClass({
getInitialState: function () {
    socket.on("new",this.Updatepost);
    return {posts: [], hasMore: true, onView: 5};
},
componentWillMount: function () {
    this.state.posts.push(this.props.newposts);
    this.setState({posts: this.props.newposts});
},
$.ajax({
    url: 'load_posts.php',
    dataType: 'json',
    success: function (data) {
        var x = data;
        React.render(<Postlist newposts={x} />,document.getElementById("main"));
    }.bind(this),
    error: function (xhr, status, err) {
        console.error(this.props.url, status, err.toString());
    }.bind(this)
});

but iam using history api to navigate between pages

so how can i unmount and remount react component when navigating and how to unmounting it outside react code

Upvotes: 1

Views: 911

Answers (1)

fagurto
fagurto

Reputation: 11

i don't think you can do that , react handles the lifecycle of a component by itself , so i think the way to work around this should be something like actually save the previous state within the browser history .. so when you go back and forth you can tell react that should update by pushing a "new" state

Upvotes: 1

Related Questions