Giter VIP home page Giter VIP logo

Comments (6)

alicoding avatar alicoding commented on June 12, 2024

Hi,

Great question! In React there are multiple ways you can go about doing what you want.

One way we are doing in on of our production application is following this pattern

const Routes = () => (
      <Main>
        <Switch>
          <Route exact path="/" component={HomePage} />
          <Route path="/another-sub-page" component={JustAnotherPage} />
          <Route path="/about-page" component={About} />
        </Switch>
      </Main>
    );

Now, Main component is basically a wrapper to our application which is just a header and a footer and whatever inside there will be the children in this case.

from react-webpack-babel.

a-barbieri avatar a-barbieri commented on June 12, 2024

Cool!

I've updated the code, but still App doesn't contain anything, it's just a place in which I put default styles. Is it right?

import React, { Component } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";

import Header from "./header";
import Footer from "./footer";

class Main extends Component {
  render() {
    return (
      <div className="Main">
        <Header />
        {this.props.children}
        <Footer />
      </div>
    );
  }
}

class Routes extends Component {
  render() {
    return (
        <Main>
          <Route exact path="/" component={Home} />
          <Route path="/another-sub-page" component={JustAnotherPage} />
          <Route path="/about-page" component={About} />
        </Main>
    );
  }
}

export default Routes;
import React, { Component } from "react";

import "normalize.css";

import "styles/base/_main.sass"; // Global styles
import "styles/base/_common.sass"; // Global styles
import styles from "./app.sass"; // Css-module styles

class App extends Component {
  render() {
    return (
      <div className="App">
        Nothing in here...
      </div>
    );
  }
}

export default App;

from react-webpack-babel.

alicoding avatar alicoding commented on June 12, 2024

Sorry for the delay, but where do you use App component right now?

from react-webpack-babel.

a-barbieri avatar a-barbieri commented on June 12, 2024

I don't... I tried to delete it but it throws a error.

[./src/index.jsx] ./src/index.jsx 2.64 kB {0} [built]
[./src/routes.jsx] ./src/routes.jsx 3.86 kB {0} [built]
    + 573 hidden modules

ERROR in ./src/index.jsx
Module not found: Error: Can't resolve './app' in '/Users/xxx/yyy/zzz/src'
 @ ./src/index.jsx 72:2-91:4 73:18-34
 @ multi (webpack)-dev-server/client?http://127.0.0.1:8080 webpack/hot/dev-server core-js/fn/promise core-js/es6/object core-js/es6/array react-hot-loader/patch ./src/index.jsx
Child html-webpack-plugin for "index.html":
     1 asset

I think it's due to this lines:

// ./index.jsx
// Hot Module Replacement API
if (module.hot) {
  module.hot.accept("./app", () => {
    const NextApp = require("./app").default;
    ReactDOM.render(
      <AppContainer>
        <Router history={history}>
          <NextApp />
        </Router>
      </AppContainer>,
      document.getElementById("app")
    );
  });
}

Honestly I haven't investigated much as it's already a pretty stiff learning curve understanding React basic steps. I don't even know if my app is making use of it.

Can I just replace ./app with another file? Maybe ./home? Not sure what is its purpose.

from react-webpack-babel.

a-barbieri avatar a-barbieri commented on June 12, 2024

I've just seen @LeonardoGentile has modified those lines 4 days ago in the master repo.

// Webpack Hot Module Replacement API
if (module.hot) {
  module.hot.accept('./routes', () => {
    renderApp(require('./routes').default);
  })
}

I've updated my code accordingly and now I'm able to ditch app.jsx.

from react-webpack-babel.

a-barbieri avatar a-barbieri commented on June 12, 2024

BTW the main class in the solution I posted above leads to the Block Updating issue. To solve this read this guide. Actually this isn't true. Without knowing it I was mutating the state in my reducer.

Wrong. Mutates previous state object.

// someReducer.js
return { ...newState, ...oldState };

Right. Generates new object.

// someReducer.js
import _ from "lodash";
return _.merge(newState, oldState);

from react-webpack-babel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.