reflux / refluxjs

A simple library for uni-directional dataflow application architecture with React extensions inspired by Flux
BSD 3-Clause "New" or "Revised" License
5.36k stars 330 forks source link

reflux use with react-router, store is not run init() when router change back. #501

Closed mcdyzg closed 7 years ago

mcdyzg commented 7 years ago

I have two pages: 1.PageOne 2.PageTwo,when i switch to pagetwo from pageone, and then switch back, i found that my PageOne's store doesn't run init().

Here is my code:

app.js:

import React,{Component} from  'react'
import ReactDOM from 'react-dom'
import {Router,hashHistory} from 'react-router'

//page
import PageOne from '../pages/PageOne'
import PageTwo from '../pages/PageTwo'

class App extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>
                {this.props.children}
            </div>
        )
    }
}
const routes = {
    path: '/',
    component: App,
    indexRoute: {component: PageOne},
    childRoutes: [
        {path: '/pageTwo', component: PageTwo}
    ]
};
ReactDOM.render(<Router history={hashHistory} routes={routes}/>, document.getElementById('app'));

store.js(PageOne):

import { createStore } from 'reflux';
import Action from './action';

export default createStore({

    listenables: [Action],

    init() {
        this.state = {
            list:[]
        };
        console.log('init')
    },

    onGetList(){
        const t = this;
        t.state.list = t.state.list.concat([1,2,3]);
        t.trigger(t.state);
    }
});

action.js(PageOne):

import Reflux from 'reflux';

export default Reflux.createActions([
    'getList',
]);

scene

open localhost:8081/#/ first time

qq 20161216142403

open localhost:8081/#/pageTwo

qq 20161216142744

open localhost:8081/#/ second time

qq 20161216142858

it seems like when switch back to #/, pageOne's store did not run init().

Help me please.

BryanGrezeszak commented 7 years ago

I may be confusing something, but I don't see why you would expect it to run init again.

init is what is called upon creation of the store. Basically like a constructor. At no point in that code is there anything that would destroy the store and then create it again.