Using React in a multi-page app

后端 未结 6 930
滥情空心
滥情空心 2021-01-29 17:21

I have been playing around with React and so far I really like it. I am building an app with NodeJS and would like to use React for some of the interactive components across the

6条回答
  •  忘掉有多难
    2021-01-29 18:09

    You can provide several entry points for the application in the webpack.config.js file:

    var config = {
      entry: {
        home: path.resolve(__dirname, './src/main'),
        page1: path.resolve(__dirname, './src/page1'),
        page2: path.resolve(__dirname, './src/page2'),
        vendors: ['react']
      },
     output: {
        path: path.join(__dirname, 'js'),
        filename: '[name].bundle.js',
        chunkFilename: '[id].chunk.js'
      },
    }
    

    then you can have in your src folder three different html files with their respective js files (example for page1):

    
    
    
      
      Page 1
    
    
      

    JavaScript file:

    import React from 'react'
    import ReactDom from 'react-dom'
    import App from './components/App'
    import ComponentA from './components/ReactComponentA'
    ReactDom.render(
    , document.getElementById('app'))

    Different React components can be then loaded for each single page.

提交回复
热议问题