导航时反应卸载

时间:2015-02-13 14:57:18

标签: url-routing reactjs html5-history

我有这个反应组件,当我渲染时它会起作用

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)
});

但我使用历史api在页面之间导航

那么我如何在导航时卸载并重新安装反应组件以及如何在反应代码外卸载它

1 个答案:

答案 0 :(得分:1)

我认为你不能这样做,反应自己处理组件的生命周期,所以我认为解决这个问题的方法应该是实际上保存浏览器历史记录中的先前状态..所以当你来回走动时,你可以通过推动一个&#34; new&#34;状态

相关问题