React






windows scripts
"build": "set NODE_ENV=production && webpack --config ./webpack.production.config.js --progress".
"scripts": {
  "start": "set NODE_ENV=dev && webpack-dev-server --progress --colors",
  "mock": "node --harmony ./mock/server.js",
  "build": "rm -rf ./build && set NODE_ENV=production && webpack --config ./webpack.production.config.js --progress --colors"
}

Create React App
Create React App  is the best way to start building a new React single page application. It sets up your development environment so that you can use the latest JavaScript features, provides a nice developer experience, and optimizes your app for production. You’ll need to have Node >= 6 on your machine.
npm install -g create-react-appcreate-react-app my-appcd my-appnpm start


Create React App doesn’t handle backend logic or databases; it just creates a frontend build pipeline, so you can use it with any backend you want. It uses build tools like  Babel  and  webpack  under the hood, but works with zero configuration.
When you’re ready to deploy to production, running  npm run build  will create an optimized build of your app in the  build  folder. You can learn more about Create React App  from its README  and the  User Guide .
React JSX
var names = ['Alice', 'Emily', 'Kate'];
ReactDOM.render(  
<div>  {    
names.map(function (name) {      
    return <div>Hello, {name}!</div>    
    }) } 
 </div>, 
 document.getElementById('example'));

JSX 的基本语法规则:遇到 HTML 标签(以  <  开头),就用 HTML 规则解析;遇到代码块(以  {  开头),就用 JavaScript 规则解析。上面代码的运行结果如下。


添加组件属性,有一个地方需要注意,就是  class  属性需要写成  className  , for  属性需要写成  htmlFor  ,这是因为  class  和  for  是 JavaScript 的保留字。


组件的生命周期
组件的 生命周期 分成三个状态:
  • Mounting:正在插入真实 DOM
  • Updating:正在被重新渲染
  • Unmounting:正在被移出真实 DOM
React 为每个状态都提供了两种处理函数, will  函数在进入状态之前调用, did  函数在进入状态之后调用,三种状态共计五种处理函数。
  • componentWillMount()
  • componentDidMount()
  • componentWillUpdate(object nextProps, object nextState)
  • componentDidUpdate(object prevProps, object prevState)
  • componentWillUnmount()

此外,React 还提供两种特殊状态的处理函数。
  • componentWillReceiveProps(object nextProps):已加载组件收到新的参数时调用
  • shouldComponentUpdate(object nextProps, object nextState):组件判断是否重新渲染时调用
这些方法的详细说明,可以参考 官方文档

Official Docs:
React is pretty flexible but it has a single strict rule:
All React components must act like pure functions with respect to their props.

Do Not Modify State Directly
For example, this will not re-render a component:
// Wrongthis.state.comment = 'Hello';
Instead, use  setState() :
// Correctthis.setState({comment: 'Hello'});
The only place where you can assign  this.state  is the constructor.


// Correctthis.setState((prevState, props) => ({ counter: prevState.counter + props.increment}));


this 指针
方式一:bind(this)

     constructor(props) { super(props); this.state = {isToggleOn: true}; // This binding is necessary to make `this` work in the callback this.handleClick = this.handleClick.bind(this); }

handleClick() { this.setState(prevState => ({ isToggleOn: !prevState.isToggleOn })); }


方式二:改为 public class fields syntax
// This syntax ensures `this` is bound within handleClick. // Warning: this is *experimental* syntax. handleClick = () => { console.log('this is:', this); }


A good rule of thumb is that elements inside the  map()  call need keys.
const listItems = numbers.map((number) => // Correct! Key should be specified inside the array. <ListItem key={number.toString()} value={number} /> );



Prop Types
//---Prop Types
const tweetPropType = PropTypes.shape({
  id: PropTypes.number.isRequired,
  text: PropTypes.string.isRequired,
  author: PropTypes.shape({
    id: PropTypes.number.isRequired,
    username: PropTypes.string.isRequired,
    firstName: PropTypes.string,
    lastName: PropTypes.string,
  }).isRequired
});
//---Components
const FullTweet = ({tweet}) => {...};
FullTweet.propTypes = {
  tweet: tweetPropType
};
const PreviewTweet = ({tweet}) => {...};
PreviewTweet.propTypes = {
  tweet: tweetPropType
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值