How to properly store React components in separate files and import React?

前端 未结 2 507
无人共我
无人共我 2021-02-04 21:33

I\'ve completed a few intro to React tutorials and am trying to put some of my knowledge so far to use. I\'ve successfully created some components inside of a

2条回答
  •  独厮守ぢ
    2021-02-04 21:42

    If you are just learning react then your way of doing using script tag is inside html fine.

    
    
    

    If you want to develop an application which can be deployed to production you need to follow following steps. No doubt there are much better tutorial available over the internet but it will give you some idea.

    1.Need Browserfiy or Webpack:

    In browsers you can not require or import modules as you usually do while writing node.js code. With the help of Browserify/Webpack you can write code that uses require/import in the same way that you would use it in node environment. I am assuming you will use webpack considering its popularity.

    2. Install dependencies (es6)

    These are minimal dependencies you need in your project (package.json) to get it working

      "devDependencies": {
            "babel-cli": "^6.3.17",
            "babel-core": "^6.3.21",
            "babel-eslint": "^5.0.0-beta6",
            "babel-loader": "^6.2.0",
            "babel-preset-es2015": "^6.3.13",
            "babel-preset-react": "^6.3.13",
            "babel-preset-stage-3": "^6.3.13",
            "css-loader": "^0.23.0",
            "eslint": "^1.10.3",
            "eslint-loader": "^1.1.1",
            "eslint-plugin-react": "^3.12.0",
            "style-loader": "^0.13.0",
            "webpack": "^1.12.9",
            "webpack-dev-server": "^1.14.0"
          },
          "dependencies": {
            "react": "^15.0.0-rc.1",
        "react-dom": "^15.0.0-rc.1"
    

    3.Write your webpack-config.js file

    A sample webpack config file should like this. Don't ask me about each bit of it but rather have a look on webpack tutorial because I can not explain everything here. Just remember the fact that Webpack is a module bundler that bundles javascript and other assets for the browser.

    var path    = require('path');
    var webpack = require('webpack');
    
    module.exports = {
      devtool: 'source-map',
      entry: {
        main: [
          'webpack-dev-server/client?http://localhost:8080',
          'webpack/hot/only-dev-server',
          './src/index.js'
        ]
      },
      output: {
        path: path.join(__dirname, 'public'),
        publicPath: 'http://localhost:8080/public/',
        filename: 'bundle.js'
      },
      plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
      ],
      module: {
       loaders: [
          {
            test      : /\.jsx?$/,
            include   : path.join(__dirname, 'src'),
            loader    : 'react-hot!babel'
          },
          {
            test      : /\.scss$/,
            include   : path.join(__dirname, 'sass'),
            loaders   : ["style", "css?sourceMap", "sass?sourceMap"]
          },
          {
              test    : /\.(png|jpg|svg)$/,
              include : path.join(__dirname, 'img'),
              loader  : 'url-loader?limit=30000&name=images/[name].[ext]'
         } // inline base64 URLs for <=30k images, direct URLs for the rest
        ]
      },
      resolve: {
        extensions: ['', '.js', '.jsx']
      },
      devServer: {
        historyApiFallback: true,
        contentBase: './'
      }
    };
    

    4.Set up entry point and routes for your application

    src->index.js src->routes.js

    index.js

    import React from 'react';
    import ReactDOM from 'react-dom';
    import {Router,browserHistory} from 'react-router';
    import routes from './routes';
    
    ReactDOM.render(
        
      , document.querySelector('.init')
    );
    

    routes.js

    import React from 'react';
    import { Route, IndexRoute } from 'react-router';
    import App from './components/app';
    import Home from './components/home';
    
    
    module.exports = (
           
               
           
    )
    

    5.Setup index.html in your project root

    
    
      
            
            
            
            Welcome to ReactJs
      
      
        

    6.Running

    Form your project root type

    webpack-dev-server --progress --colors
    

    import and require

    import and require are very similar in functionality. Only difference is that import is new syntactic sugar available with es6 while require is used with es5.

提交回复
热议问题