首先npm和node环境就不说了,不懂得参考我写的vue2.0安装,里面有npm和node安装的介绍
react开发
参考官网创建react项目搭建开发环境,安装的是最新版
npx create-react-app my-app(这个是项目名称)
安装完成后cd my-app 直接npm run start 看到运行之后的效果

 

先按官网走一遍基础吧
JSX简介
JSX是一个Javascript的语法扩展,

了解下jsx编译

Babel 会把 JSX 转译成一个名为 React.createElement() 函数调用。
const element = (
   <div className="box">
     <div className="title">hello</div>
     <button>click</button>
   </div> 
);//()如果存在标签结构,并且标签结构要换行,就需要这个

const element = React.createElement(
   "div",
   {className:"box"},
   React.createElement(
      "div",
      {className:"title"},
      "hello"
   ),
   React.createElement(
      "button",
      null,
      "click"
   )
);
每个 DOM 元素的结构都可以用 JavaScript 的对象来表示
{
    tag:'div',
    attrs:{className:'box'},
    children:[
       {
           tag:'div',
           attrs:{className:'title'},
           children:['hello']
        },
        {
            tag:'button',
            attrs:null,
            children:['click']
        }
    ]  
}
View Code

警告:

因为 JSX 语法上更接近 JavaScript 而不是 HTML,所以 React DOM 使用 camelCase(小驼峰命名)来定义属性的名称,而不使用 HTML 属性名称的命名约定。

例如,JSX 里的 class 变成了 className,而 tabindex 则变为 tabIndex

ReactDom.render  渲染dom树
最简单的组件模板

注意: 组件名称必须以大写字母开头。

React 会将以小写字母开头的组件视为原生 DOM 标签。例如,<div /> 代表 HTML 的 div 标签,而 <Welcome /> 则代表一个组件,并且需在作用域内使用 Welcome

//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
    <App />,
  document.getElementById('root')
);

//App
import React from 'react';
class App extends React.Component{
  constructor(props){
    super(props);
    this.state={
      aa:'hello world'
    }
  }
  render(){
    return(
      <div>
        <h1>hello world</h1>
        <div>{this.state.aa}</div>
      </div>
    )
  }
}
export default App;
View Code

 vscode两个快捷键
imr     import React from 'react'
ccc  快速生成基础模板

state

/*react通过页面元素改变使用state进行处理*/

import React from 'react'

class StateCompomemt extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 
            count:10
        }
        /*绑值方法三*/
        this.increment=this.increment.bind(this);
    }
    
    render() { 
        return ( 
            <div>
                <h3>组件的state</h3>
                <p>{this.state.count}</p>
                {/*绑值方法一 bind绑定*/}
                {/* <button onClick={this.increment.bind(this)}>增加</button> */}
                {/*绑值方法二 直接箭头函数*/}
                {/* <button onClick={()=>this.increment()}>增加</button> */}
                {/*箭头函数另一种*/}
                <button onClick={this.increment}>增加</button>
                {/*绑值方法三*/}
                <button onClick={this.increment}>增加</button>
            </div>
        );
    }
    increment(){
        this.setState({
            count:this.state.count+=1
        })
    }
    /*箭头函数另一种写法*/
    increment=()=>{
       this.setState({
            count:this.state.count+=1
        })
   }
}
 
export default StateCompomemt;        
View Code

以上介绍了3种事件绑定的方法,推荐使用箭头函数的方式
生命周期就不说了,我有一篇随便写过了

setState是同步还是异步的??

render() { 
    return ( 
        <div>
            <p>setState是同步还是异步</p>
            <p>{this.state.count}</p>
            <button onClick={this.increment}>增加</button>
        </div>
    );
}
increment=()=>{
    this.setState({
        count:this.state.count+1
    })
    console.log("dd",this.state.count)
}

 //改同步

<button onClick={this.increment.bind(this)}>增加</button>

 async increment(){
     await this.setStateAsync({count:this.state.count+1})
  }

  setStateAsync(state){

    return new Promise((resolve)=>{

      this.setState(state,resolve);

    })

  }

 

 事实证明是异步的

官方给出的解释是,State 的更新可能是异步的,

出于性能考虑,React 可能会把多个 setState() 调用合并成一个调用。

因为 this.props 和 this.state 可能会异步更新,所以你不要依赖他们的值来更新下一个状态。

 事件处理条件渲染列表,这个参考我的另一篇react有介绍
表单另一篇也做了基本介绍

propTypes进行类型检查

官方声明注意:

自 React v15.5 起,React.PropTypes 已移入另一个包中。请使用 prop-types 库 代替。

import PropTypes from 'prop-types';

class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

Greeting.propTypes = {
  name: PropTypes.string  //传入值声明数据类型,类似typescript
};
// 指定 props 的默认值:
Greeting.defaultProps = {
  name: 'Stranger'
};
 
import PropTypes from 'prop-types';
MyComponent.propTypes = {
    optionalArray: PropTypes.array, //数组
    optionalBool: PropTypes.bool, //boolean
    optionalFunc: PropTypes.func,  
    optionalNumber: PropTypes.number,
    optionalObject: PropTypes.object,
    optionalString: PropTypes.string,
    optionalSymbol: PropTypes.symbol,
    // 任何可被渲染的元素(包括数字、字符串、元素或数组)
  // (或 Fragment) 也包含这些类型。
  optionalNode: PropTypes.node,

  // 一个 React 元素。
  optionalElement: PropTypes.element,

  // 一个 React 元素类型(即,MyComponent)。
  optionalElementType: PropTypes.elementType,

  // 你也可以声明 prop 为类的实例,这里使用
  // JS 的 instanceof 操作符。
  optionalMessage: PropTypes.instanceOf(Message),

  // 你可以让你的 prop 只能是特定的值,指定它为
  // 枚举类型。
  optionalEnum: PropTypes.oneOf(['News', 'Photos']),

  // 一个对象可以是几种类型中的任意一个类型
  optionalUnion: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
    PropTypes.instanceOf(Message)
  ]),

  // 可以指定一个数组由某一类型的元素组成
  optionalArrayOf: PropTypes.arrayOf(PropTypes.number),

  // 可以指定一个对象由某一类型的值组成
  optionalObjectOf: PropTypes.objectOf(PropTypes.number),

  // 可以指定一个对象由特定的类型值组成
  optionalObjectWithShape: PropTypes.shape({
    color: PropTypes.string,
    fontSize: PropTypes.number
  }),
  
  // An object with warnings on extra properties
  optionalObjectWithStrictShape: PropTypes.exact({
    name: PropTypes.string,
    quantity: PropTypes.number
  }),   

  // 你可以在任何 PropTypes 属性后面加上 `isRequired` ,确保
  // 这个 prop 没有被提供时,会打印警告信息。
  requiredFunc: PropTypes.func.isRequired,

  // 任意类型的数据
  requiredAny: PropTypes.any.isRequired,

  // 你可以指定一个自定义验证器。它在验证失败时应返回一个 Error 对象。
  // 请不要使用 `console.warn` 或抛出异常,因为这在 `onOfType` 中不会起作用。
  customProp: function(props, propName, componentName) {
    if (!/matchme/.test(props[propName])) {
      return new Error(
        'Invalid prop `' + propName + '` supplied to' +
        ' `' + componentName + '`. Validation failed.'
      );
    }
  },

  // 你也可以提供一个自定义的 `arrayOf` 或 `objectOf` 验证器。
  // 它应该在验证失败时返回一个 Error 对象。
  // 验证器将验证数组或对象中的每个值。验证器的前两个参数
  // 第一个是数组或对象本身
  // 第二个是他们当前的键。
  customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
    if (!/matchme/.test(propValue[key])) {
      return new Error(
        'Invalid prop `' + propFullName + '` supplied to' +
        ' `' + componentName + '`. Validation failed.'
      );
    }
  })
} //官网介绍
View Code

 

 先写到这里,后期持续更新。。。

 

posted on 2020-05-31 10:40  执候  阅读(272)  评论(0编辑  收藏  举报