react 详细解析学习笔记
<p><span style="font-size: 14pt"><strong>React的介绍:</strong></span></p><p> React来自于Facebook公司的开源项目</p>
<p> React 可以开发单页面应用 spa(单页面应用)</p>
<p> react 组件化模块化 开发模式</p>
<p> React通过对DOM的模拟(虚拟dom),最大限度地减少与DOM的交互 (数据绑定)</p>
<p> react灵活 React可以与已知的库或框架很好地配合。</p>
<p> react 基于jsx的语法,JSX是React的核心组成部分,它使用XML标记的方式去直接声明界面, html js混写模式</p>
<p><span style="font-size: 14pt"><strong>搭建React开发环境之前的准备工作。</strong></span></p>
<p> 1、必须安装nodejs 注意:安装nodejs稳定版本</p>
<p> 2、安装cnpm用cnpm替代npm</p>
<p> 地址:http://npm.taobao.org/</p>
<p> 安装cnpm:</p>
<p> npm install -g cnpm --registry=https://registry.npm.taobao.org</p>
<p> 3、用yarn替代npm</p>
<p> yarn的安装:</p>
<p> 第一种方法:参考官方文档https://yarn.bootcss.com/</p>
<p> 第二种方法:cnpm install -g yarn 或者 npm install -g yarn</p>
<p><span style="font-size: 14pt"><strong>搭建React开发环境</strong></span></p>
<p><span style="font-size: 15px"><strong>第一种方法(老-现在推荐):</strong></span></p>
<p> https://reactjs.org/docs/create-a-new-react-app.html</p>
<p> 1、必须要安装nodejs 注意:安装nodejs稳定版本 nodejs版本:v8.11.2 npm版本:v5.6.0</p>
<p> 2.安装脚手架工具 (单文件组件项目生成工具) 只需要安装一次</p>
<p> npm install -g create-react-app / cnpm install -g create-react-app</p>
<p> 3.创建项目 (可能创建多次)</p>
<p> 找到项目要创建的目录:</p>
<p> create-react-app reactdemo</p>
<p> 4.cd 到项目里面</p>
<p> cd reactdemo</p>
<p> npm start yarn start运行项目</p>
<p> npm run build yarn build 生成项目</p>
<p><span style="font-size: 14px"><strong>第二种方法(新-未来推荐):</strong></span></p>
<p> https://reactjs.org/docs/create-a-new-react-app.html</p>
<p> 1、必须要安装nodejs 注意:安装nodejs稳定版本 nodejs版本:v8.11.2 npm版本:v5.6.0</p>
<p> 2.安装脚手架工具并创建项目</p>
<p> 找到项目要创建的目录执行:</p>
<p> npx create-react-app reactdemo</p>
<p> 4.cd 到项目里面</p>
<p> cd reactdemo</p>
<p> npm start 运行项目(调试)</p>
<p> npm run build 生成项目(发布)</p>
<p><strong>npx介绍:</strong></p>
<p> npm v5.2.0引入的一条命令(npx),引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验。</p>
<p>详情:</p>
<p> http://www.phonegap100.com/thread-4910-1-1.html</p>
<p> npx create-react-app reactdemo这条命令会临时安装 create-react-app 包,命令完成后create-react-app 会删掉,不会出现在 global 中。下次再执行,还是会重新临时安装。</p>
<p> npx 会帮你执行依赖包里的二进制文件。</p>
<p> 再比如 npx http-server 可以一句话帮你开启一个静态服务器</p>
<p> </p>
<p><strong>manifest.json 文件简介:</strong></p>
<p> https://lavas.baidu.com/mip/doc/engage-retain-users/add-to-home-screen/introduction</p>
<p> 允许将站点添加至主屏幕,是 PWA 提供的一项重要功能,当前 manifest.json 的标准仍属于草案阶段,Chrome 和 Firefox 已经实现了这个功能,微软正努力在 Edge 浏览器上实现,Apple 目前仍在考虑中</p>
<p><strong>super关键字:</strong></p>
<p> 参考:http://www.phonegap100.com/thread-4911-1-1.html</p>
<p> Es6中的super可以用在类的继承中,super关键字,它指代父类的实例(即父类的this对象)。子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)"> class Person {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)"> constructor (name) {
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)"> class Student extends Person {
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)"> constructor (name, age) {
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> super(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 用在构造函数中,必须在使用this之前调用</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">11</span> }</pre>
</div>
<p> </p>
<p><strong>为什么官方的列子里面写个super(props):</strong></p>
<p> 只有一个理由需要传递props作为super()的参数,那就是你需要在构造函数内使用this.props</p>
<p>那官方提供学习的例子中都是写成super(props),所以说写成super(props)是完全没问题的,也建议就直接这样写。</p>
<p> </p>
<p><span style="font-size: 14pt"> <strong>ReactJSX语法</strong></span></p>
<p><span style="font-size: 16px; color: rgba(0, 0, 0, 1)"> JSX就是Javascript和XML结合的一种格式。React发明了JSX,可以方便的利用HTML语法来创建虚拟DOM,当遇到<code><</code>,JSX就当作HTML解析,遇到<code>{</code>就当JavaScript解析.</span></p>
<p>1、所有的模板要被一个根节点包含起来,在render函数中return返回的只能包含<strong>一个顶层标签</strong>,否则也会报错</p>
<p>2、模板元素不要加引号</p>
<p>3、JSX基本语法规则,遇到HTML标签(以<开头),就用HTML规则解析;遇到代码块(以{开头),就用JS规则解析 </p>
<p>4、绑定属性注意:</p>
<p> class 要变成 className </p>
<p> for 要变成 htmlFor</p>
<p> style属性和以前的写法有些不一样</p>
<p> <div style={{'color':'blue'}}>{this.state.title}</div></p>
<p> <div style={{'color':this.state.color}}>{this.state.title}</div></p>
<p>5、循环数据要加key</p>
<p>6、组件的构造函数中一定要注意 super</p>
<p> 子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">constructor(props){
</span><span style="color: rgba(0, 128, 128, 1)">2</span> super(props);<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">用于父子组件传值固定写法</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">this</span>.state=<span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)">4</span> userinfo:'张三'
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">6</span> }</pre>
</div>
<p> </p>
<p>7、组件名称首字母大写、组件类名称首字母大写</p>
<p>8、react解析html</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <div dangerouslySetInnerHTML={{__html: <span style="color: rgba(0, 0, 255, 1)">this</span>.state.list.htmlStr}}> </div></pre>
</div>
<p>9、条件判断的四种写法</p>
<p> 用<strong>三元表达式</strong><strong>,</strong> 使用dom元素<strong>变量,</strong>直接调用<strong>函数,</strong>使用<strong>逻辑运算符,</strong>利用即时执行函数</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)"> class HelloWorld extends React.Component{
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)"> render(){
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">return</span> <p><span style="color: rgba(0, 0, 0, 1)">Hello {
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> (<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(obj){
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(obj.props.name){
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> obj.props.name;
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> }<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 255, 1)">return</span> "World"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">10</span> }(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 128, 128, 1)">11</span> }</p>
<span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">13</span> }</pre>
</div>
<p> </p>
<h3 id="fragment标签讲解">Fragment标签</h3>
<p> 加上最外层的DIV,组件就是完全正常的,但是你的布局就偏不需要这个最外层的标签怎么办?比如我们在作<code>Flex</code>布局的时候,外层还真的不能有包裹元素。这种矛盾其实React16已经有所考虑了,为我们准备了<code><Fragment></code>标签。</p>
<p>在以类继承的方式定义的组件中,为了能方便地调用当前组件的其他成员方法或属性(如:this.state),通常需要将事件处理函数运行时的 this 指向当前组件实例。</p>
<p>引入方式:</p>
<pre class="language-javascript"><code><span class="token keyword">import React<span class="token punctuation">,<span class="token punctuation">{Component<span class="token punctuation">,Fragment <span class="token punctuation">} <span class="token keyword">from <span class="token string">'react'</span></span></span></span></span></span></span></code></pre>
<p>然后把最外层的<code><div></code>标签,换成<code><Fragment></code>标签</p>
<p> </p>
<p><span style="font-size: 14pt"><strong>setState</strong></span></p>
<p> React是禁止直接操作state的,虽然上面的方法也管用,但是在后期的性能优化上会有很多麻烦</p>
<p><span style="font-size: 14pt"><strong>绑定事件处理函数this的几种方法:</strong></span></p>
<p>第一种方法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">run(){
</span><span style="color: rgba(0, 128, 128, 1)">2</span> alert(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state.name)
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span><button onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.run.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>)}>按钮</button></pre>
</div>
<p>第二种方法: 构造函数中改变</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">this</span>.run = <span style="color: rgba(0, 0, 255, 1)">this</span>.run.bind(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">run(){
</span><span style="color: rgba(0, 128, 128, 1)">3</span> alert(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state.name)
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <button onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.run>按钮</button></pre>
</div>
<p>第三种方法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> run=()=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> alert(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state.name)
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <button onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.run>按钮</button></pre>
</div>
<p>React中的组件: 解决html 标签构建应用的不足。</p>
<p align="left"><strong>获取表单的值</strong></p>
<p align="left">(获取点击的某个键的key值 e.keyCode)</p>
<p align="left">1、监听表单的改变事件 onChange</p>
<p align="left">2、在改变的事件里面获取表单输入的值 ref获取</p>
<p align="left">3、把表单输入的值赋值给username this.setState({})</p>
<p align="left">4、点击按钮的时候获取 state里面的username this.state.username</p>
<p> </p>
<p><span style="font-size: 14pt"> <strong>组件传值<strong>:</strong></strong></span></p>
<p>使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入。</p>
<p>父子组件:组件的相互调用中,我们把调用者称为父组件,被调用者称为子组件</p>
<p><strong>父子组件传值:</strong></p>
<p> 一,父组件给子组件传值</p>
<p> 1.在调用子组件的时候定义一个属性 <Header msg='首页'></Header></p>
<p> 2.子组件里面 this.props.msg </p>
<p> 说明:父组件不仅可以给子组件传值,还可以给子组件传方法,以及把整个父组件传给子组件。</p>
<p> <strong> 记住一点:<span style="color: rgba(255, 0, 0, 1)">父组件向子组件传递内容,靠属性的形式传递</span>。</strong></p>
<p> 父组件主动获取子组件的数据</p>
<p> 1、调用子组件的时候指定ref的值 <Header ref='header'></Header> </p>
<p> 2、通过this.refs.header 获取整个子组件实例</p>
<p><strong> </strong></p>
<p>二,子组件给父组件传值:</p>
<p> defaultProps:父子组件传值中,如果父组件调用子组件的时候不给子组件传值,可以在子组件中使用defaultProps定义的默认值</p>
<p> propTypes:验证父组件传值的类型合法性,定义父组件给子组件传值的类型</p>
<p> 1、引入import PropTypes from 'prop-types';</p>
<p> 2、类.propTypes = {</p>
<p> name: PropTypes.string</p>
<p> };</p>
<p> 都是定义在子组件里面</p>
<p><strong>约束性和非约束性组件:</strong></p>
<p> 非约束性组:<input type="text" defaultValue="a" /> 这个 defaultValue 其实就是原生DOM中的 value 属性。</p>
<p> 这样写出的来的组件,其value值就是用户输入的内容,React完全不管理输入的过程。</p>
<p> 约束性组件:<input value={this.state.username} type="text" onChange={this.handleUsername} /></p>
<p> 这里,value属性不再是一个写死的值,他是 this.state.username, this.state.username 是由 this.handleChange 负责管理的。</p>
<p> 这个时候实际上 input 的 value 根本不是用户输入的内容。而是onChange 事件触发之后,由于 this.setState 导致了一次重新渲染。不过React会优化这个渲染过程。看上去有点类似双休数据绑定</p>
<p><span style="font-size: 14pt"><strong>react获取服务器APi接口的数据:</strong></span></p>
<p> react中没有提供专门的请求数据的模块。但是我们可以使用任何第三方请求数据模块实现请求数据</p>
<p> <strong> 1、axios </strong> https://github.com/axios/axios axios的作者觉得jsonp不太友好,推荐用CORS方式更为干净(后端运行跨域)</p>
<p> 1、安装axios模块npm install axios --save / npm install axios --save</p>
<p> 2、在哪里使用就在哪里引入import axios from 'axios'</p>
<p> 3、看文档使用</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> Var api=''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">axios.get(api)
</span><span style="color: rgba(0, 128, 128, 1)">3</span> .then(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (response) {
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 0, 1)"> console.log(response);
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 0, 1)">})
</span><span style="color: rgba(0, 128, 128, 1)">6</span> .<span style="color: rgba(0, 0, 255, 1)">catch</span>(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (error) {
</span><span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 0, 0, 1)"> console.log(error);
</span><span style="color: rgba(0, 128, 128, 1)">8</span> });</pre>
</div>
<p> <strong>2、fetch-jsonp</strong> https://github.com/camsong/fetch-jsonp</p>
<p> 1、安装 npm install fetch-jsonp --save</p>
<p> 2、import fetchJsonp from 'fetch-jsonp'</p>
<p> 3、看文档使用</p>
<div class="cnblogs_code">
<pre>fetchJsonp('/users.jsonp'<span style="color: rgba(0, 0, 0, 1)">)
.then(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(response) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> response.json()
}).then(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(json) {
console.log(</span>'parsed json'<span style="color: rgba(0, 0, 0, 1)">, json)
}).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(ex) {
console.log(</span>'parsing failed'<span style="color: rgba(0, 0, 0, 1)">, ex)
})</span></pre>
</div>
<p> 3、其他请求数据的方法也可以...自己封装模块用原生js实现数据请求也可以...</p>
<p> </p>
<p><strong><span style="font-size: 14pt">React生命周期函数:</span></strong></p>
<p> 组件加载之前,组件加载完成,以及组件更新数据,组件销毁。</p>
<p> 触发的一系列的方法 ,这就是组件的生命周期函数</p>
<p>组件加载的时候触发的函数:</p>
<p> constructor 、componentWillMount、 render 、componentDidMount</p>
<p>组件数据更新的时候触发的生命周期函数:</p>
<p> shouldComponentUpdate、componentWillUpdate、render、componentDidUpdate</p>
<p>你在父组件里面改变props传值的时候触发的:</p>
<p> componentWillReceiveProps</p>
<p>组件销毁的时候触发的:</p>
<p> componentWillUnmount</p>
<p>必须记住的生命周期函数:</p>
<p> *加载的时候:componentWillMount、 render 、componentDidMount(dom操作)</p>
<p> 更新的时候:componentWillUpdate、render、componentDidUpdate</p>
<p> *销毁的时候: componentWillUnmount</p>
<p> </p>
<p><span style="font-size: 14pt"><strong> react路由的配置:</strong></span></p>
<p> 1、找到官方文档 https://reacttraining.com/react-router/web/example/basic</p>
<p> 2、安装 cnpm install react-router-dom --save</p>
<p> 3、找到项目的根组件引入react-router-dom</p>
<p> import { BrowserRouter as Router, Route, Link } from "react-router-dom";</p>
<p> 4、复制官网文档根组件里面的内容进行修改 (加载的组件要提前引入)</p>
<div class="cnblogs_code">
<pre><Router>
<Link to="/">首页</Link>
<Link to="/news">新闻</Link>
<Link to="/product">商品</Link>
<Route exact path="/" component={Home} />
<Route path="/news" component={News} />
<Route path="/product" component={Product} />
</Router></pre>
</div>
<p> <strong> exact表示严格匹配</strong></p>
<p>实现js跳转路由:https://reacttraining.com/react-router/web/example/auth-workflow</p>
<p>1、要引入Redirect</p>
<p>2、定义一个flag</p>
<p> this.state = {</p>
<p> loginFlag:false </p>
<p> };</p>
<p>3、render里面判断flag 来决定是否跳转</p>
<p> if(this.state.loginFlag){</p>
<p> return <Redirect to={{ pathname: "/" }} />;</p>
<p> }</p>
<p>4、要执行js跳转</p>
<p> 通过js改变loginFlag的状态</p>
<p> 改变以后从新render 就可以通过Redirect自己来跳转</p>
<p> </p>
<p align="left"><strong>url模块来解析url地址</strong> 在react里面使用url模块需要安装url模块 cnpm install url --save</p>
<p align="left">import url from 'url';</p>
<p align="left"> //获取get传值</p>
<p align="left"> console.log(url.parse(this.props.location.search,true));</p>
<p align="left"> var query=url.parse(this.props.location.search,true).query;</p>
<p align="left"> console.log(query)</p>
<p align="left"> </p>
<p align="left"><span style="font-size: 16px"><strong> 未完待续。。。</strong></span></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/coober/p/10948612.html
頁:
[1]