0
私はアプリケーションでルーティングするために反応ルータv4を使用しています。コンポーネント、サガおよびレデューサーは非同期にロードされます。このような場合に私は今どのようにプライベートルーティングとパブリックルーティングを実装できますか?ここで反応ルータv4で非同期ルートへのプライベートルートを作成
はあなたのReduxの店であなたの歴史を同期するreact-router-redux
を使用していると仮定すると、ルーティングおよびロード
/**
* A wrapper component that will lazily render a component after it has been loaded.
*/
class Bundle extends Component {
static contextTypes = {
store: React.PropTypes.object
};
state = {
// short for "module" but that's a keyword in js, so "mod"
mod: null
};
componentWillMount() {
this.load(this.props);
}
/* istanbul ignore next */
componentWillReceiveProps(nextProps) {
if (nextProps.load !== this.props.load) {
this.load(nextProps);
}
}
load(props) {
this.setState({
mod: null
});
props.load(this.context.store, mod => {
this.setState({
// handle both es imports and cjs
mod: mod.default ? mod.default : mod
});
});
}
render() {
// eslint-disable-next-line no-unused-vars
const { load, ...otherProps } = this.props;
return this.state.mod && <this.state.mod {...otherProps} />;
}
}
const AsyncRoute = ({ load, ...others }) => (
<Route {...others} render={props => <Bundle load={load} {...props} />} />
);
AsyncRoute.propTypes = {
computedMatch: React.PropTypes.object,
path: React.PropTypes.string,
load: React.PropTypes.func
};
export default AsyncRoute;
// how can i make private route with different layout not a children of App
function Routes({ location }) {
return (
<Switch location={location}>
<AsyncRoute exact path="/" load={loadHomePage} />
<AsyncRoute exact path="/signup" load={loadSignupPage} />
<AsyncRoute path="" load={loadNotFoundPage} />
</Switch>
);
}
export default (store, cb) => {
const { injectReducer, injectSagas } = getAsyncInjectors(store);
const importModules = Promise.all([
import("./reducer"),
import("./sagas"),
import("./index")
]);
importModules.then(([reducer, sagas, component]) => {
injectReducer("signup", reducer.default);
injectSagas(sagas.default);
cb(component);
});
importModules.catch(errorLoading);
};
const render = messages => {
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>,
document.getElementById("app")
);
};
いくつかのコードをスケッチしているあなたは垣間見るを表示することができますか?私は完全に理解していませんでした。 – Serenity
コードスケッチを追加 –