真的是历经磨难,花了三四个小时,才写正确一个react component

简介: 第一次进入新的领域,令人兴奋,又令人备受折磨。 第一个接书上来的react组件,写起来也是一波三折。

第一次进入新的领域,令人兴奋,又令人备受折磨。

第一个接书上来的react组件,写起来也是一波三折。


不提class大小写,不提如何用低版本的prop-types作验证,不提在eslint里屏蔽一些问题("react/prefer-stateless-function": "off","import/no-named-as-default": 0,"import/no-named-as-default-member": 0,),不提HtmlwebpackPlugin如何生成模板,更不提空格和tab,单引号和双引号,XXXX,,啥也不提,上个文件记录,知道我曾查过很多资料,解决过这些问题。


package.json


{
  "name": "react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "dev": "webpack-dev-server --hot"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-react-hmre": "^1.1.1",
    "eslint": "^4.11.0",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-loader": "^1.9.0",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jsx": "0.0.2",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-react": "^7.5.1",
    "html-webpack-plugin": "^2.30.1",
    "webpack": "^3.8.1",
    "webpack-dev-server": "^2.9.4"
  },
  "dependencies": {
    "prop-types": "^15.6.0",
    "react": "^16.1.1",
    "react-dom": "^16.1.1"
  }
}

.babelrc


{
	"presets": ["es2015", "react"],
	"plugins": ["transform-object-rest-spread"],
	"env": {
		"development": {
			"presets": ["react-hmre"]
		}
	}
}

.eslintrc


{
	"extends": "airbnb",
	"rules": {
		"comma-dangle": ["error", "never"],
		"linebreak-style": ["error", "windows"],
		"import/no-named-as-default": 0,
		"import/no-named-as-default-member": 0,
		"react/prefer-stateless-function": "off",
	},
	"env": {
		"browser": true
	}
}

webpack.config.js


var path = require("path");
var webpack = require("webpack");
var HtmlwebpackPlugin = require("html-webpack-plugin");

var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, "app");
var BUILD_PATH = path.resolve(ROOT_PATH, "build");

module.exports = {
	entry: {
		app: path.resolve(APP_PATH, "app.jsx")
	},
	output: {
		path: BUILD_PATH,
		filename: "bundle.js"
	},
	resolve: {
		extensions: [".js", ".jsx"]
	},
	devtool: "eval-source-map",
	devServer: {
		historyApiFallback: true,
		hot: true,
		inline: true,
		progress: true
	},
	
	module: {
		rules: [
			{
				test: /\.jsx?$/,
				enforce: "pre",
				loaders: ["eslint-loader"],
				include: APP_PATH
			},
			{
				test: /\.jsx?$/,
				loaders: ["babel-loader"],
				include: APP_PATH
			}
		]
	},
	
	plugins: [
		new HtmlwebpackPlugin({
			title: "My first react app",
			template: 'my-index.ejs'
		})
	]
}

app.js


import React from 'react';
import { render } from 'react-dom';
import Profile from './Profile';

const props = {
  name: 'viking',
  age: 20
};
render(<Profile {...props} />, document.getElementById('container'));

Profile.js


import React from 'react';
import PropTypes from 'prop-types';

const propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.number.isRequired
};

export default class Profile extends React.Component {
  render() {
    return (
      <div className="profile-component">
        {/* this.props就是传入的属性 */}
        <h1>我的名字叫{this.props.name}</h1>
        <h2>我今年{this.props.age}岁</h2>
      </div>
    );
  }
}

Profile.propTypes = propTypes;

my-index.ejs


<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
  <div id="container"> </div>
  </body>
</html>

最后的END.


目录
相关文章
|
2月前
|
前端开发
react-router中的render、children、component
react-router中的render、children、component
49 1
|
4月前
|
前端开发
React Component和Purecomponent区别
React Component和Purecomponent区别
20 0
|
5月前
|
弹性计算 前端开发 JavaScript
前端新趋势?有了Web Component,还用纠结Vue或React?
Web Component 的概念最早在 2011 年被 Google 提出,并在 2018 年 V1 版本开始被主流浏览器所支持(除了 IE)。
|
9月前
|
前端开发
前端项目实战玖拾贰react-admin+material ui-踩坑-List的用法之component用法
前端项目实战玖拾贰react-admin+material ui-踩坑-List的用法之component用法
39 0
|
前端开发 测试技术 API
在 Shopify 探索 React Server Component 的最佳实践
Shopify 是国外的一个允许客户自由搭建商城的 no code 产品,工程师 Cathryn Griffiths 分享了他在 Shopify 中实用 React Server Component 的最佳实践。
在 Shopify 探索 React Server Component 的最佳实践
|
前端开发 JavaScript Java
SSR在左,React Server Component在右
SSR在左,React Server Component在右
172 0
SSR在左,React Server Component在右
|
前端开发 JavaScript Android开发
React Native之组件(Component)生命周期学习笔记
React Native之组件(Component)生命周期学习笔记
141 0
React Native之组件(Component)生命周期学习笔记
|
前端开发
如何在SAP Fiori应用里使用React component
如何在SAP Fiori应用里使用React component
如何在SAP Fiori应用里使用React component
|
前端开发
SAP UI5 Web Component React应用如何在Component之间跳转
SAP UI5 Web Component React应用如何在Component之间跳转
123 0
SAP UI5 Web Component React应用如何在Component之间跳转
|
前端开发
SAP UI5 Web Component不同React页面的跳转实现
SAP UI5 Web Component不同React页面的跳转实现
119 0
SAP UI5 Web Component不同React页面的跳转实现