创建主要组件,读取所有其他组件,以便它们继承.container

时间:2018-03-01 00:18:16

标签: reactjs react-router-v4 react-router-redux react-router-dom

"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router-dom": "^4.1.2",
"react-router-redux": "^5.0.0-alpha.9",

我一直在研究一个React项目已经有一段时间了,刚才我意识到我所拥有的app.js“master”组件根本没有做任何事情。我的印象是我的所有其他组件都是通过它阅读的。 (一旦我意识到,就像是,“哦,是的,为什么会这样?”)。

这种方式给我带来了一个问题:我需要在每个组件中添加.container.container-fluid标签,或者我需要将它们放在index.js中。我想避免这两种情况:前者由于冗余而后者由于希望保持格式化index.js

那么我该如何重构这些附加到这些路线的组件是通过“主”组件发送的,这样可以应用这些格式标签?

这是我的index.js和我的app.js(原来根本没有做任何事情):

// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import { routerMiddleware, ConnectedRouter } from 'react-router-redux';

import createHistory from 'history/createBrowserHistory';

// Render on every route
import App from './components/app';
import Navigation from './containers/global/navbar';
import Footer from './containers/global/footer';

// Route specific
import Signin from './containers/authentication/signin';
import Signout from './containers/authentication/signout';
import ResetPassword from './containers/authentication/reset_password';
import SetPassword from './containers/authentication/set_password';
import RecoverUsername from './containers/authentication/recover_username';
import NewAccount from './containers/authentication/new_account';
import SetupUser from './containers/authentication/setup_user';
import SecurityQuestions from './containers/authentication/security_questions';
import Home from './components/home';
import Help from './components/help';

// HoC to wrap protected routes in
import Timers from './helpers/timers';
import RequireAuth from './helpers/require_auth';

// Reducers 
import rootReducer from './reducers';

// Global app styles
import styles from '../assets/scss/app.scss';

// IE polyfill error fix
require('es6-promise').polyfill();
var axios = require('axios');

const history = createHistory();

const initialState = {};
const enhancers = [];
const middleware = [thunk, routerMiddleware(history)];

if (process.env.NODE_ENV === 'development') {
    const devToolsExtension = window.devToolsExtension

    if (typeof devToolsExtension === 'function') {
        enhancers.push(devToolsExtension())
    }
}

const composedEnhancers = compose(applyMiddleware(...middleware), ...enhancers);
const protectedRoute = compose(Timers, RequireAuth);

const store = createStore(rootReducer, initialState, composedEnhancers);

ReactDOM.render(
    <Provider store={store}>
        <ConnectedRouter history={history}>
            <div>
                <Navigation />
                <App />
                <Switch>
                    <Route exact path='/home' component={protectedRoute(Home)} />
                    <Route exact path='/help' component={protectedRoute(Help)} />
                    <Route exact path='/auth/username' component={RecoverUsername} />
                    <Route exact path='/auth/new_account' component={NewAccount} />
                    <Route exact path='/auth/password' component={ResetPassword} />
                    <Route exact path='/auth/signout' component={Signout} />
                    <Route exact path='/auth/signin' component={Signin} />

                    <Route exact path='/auth/security_questions/f=:f&i=:id&k=:key' component={SecurityQuestions} />
                    <Route exact path='/auth/set_password/f=:f&i=:id&k=:key' component={SetPassword} />
                    <Route exact path='/auth/setup_user/f=:f&i=:id&k=:key' component={SetupUser} />
                    <Route exact path='/' component={Signin} />
                </Switch>
                <div id='gap'></div>
                <Footer />
            </div>
        </ConnectedRouter>
    </Provider>
    , document.querySelector('.app'));
// app.js
import React, { Component } from 'react';

export default class App extends Component {
    render() {
        return (
            <div>
                {this.props.children}
            </div>
        )
    }
}

1 个答案:

答案 0 :(得分:0)

好的,这是一个非常直接的事情,非常直观......

index.js我刚刚将<App />更改为:

<App>
    <Switch>
    // all the routes...
    </Switch
</App>

按照人们的预期工作。