我的react学习
基础部分
创建一个react的项目
创建一个react的项目
全局安装 react 指令
// 全局安装react (根据需要安装,不是必须的)
npm i -g react // 或者 yarn -global react // 全局安装 react的脚手架 (创建react的应用,必须安装脚手架)
npm i -g create-react-app // 或者 yarn -global create-react-app
使用脚手架创建应用
create-react-app 应用名称
// 例如创建一个TodoList应用
create-react-app todo-list
注意点:
1.全局安装create-react-app才能使用脚手架创建应用
2.应用名称全部为小写字母,不能出现“TodoList”这样的名称
组件的使用
组件的组成
// React 是react组件必不可少的,用于支持jsx语法的模块,虽然看不到引用,但是不能少
// Component 所有的react的组件都继承于Component,它是react组件的基类
import React, { Component } from 'react'; // TodoItem 自定义的组件
import TodoItem from "./TodoItem" // 定义TodoList组件 ,该组件继承于基类Component
class TodoList extends Component {
/***
* constructor 当前类的构造函数
* props 简单的理解:是父类传递的参数,例如 传递的值 或 方法,它是一个对象
* super(props) 简单的理解是:继承父类中的传递的参数
**/
constructor(props){
super(props)
// state 是 所有react变量的仓储,简单理解就是:当前组件的变量都放在这个对象中
this.state = {
inputValue: "",
List: []
}
// 使用 bind绑定this,让方法中的this永远指向当前的组件(可根据需求改变this的指向)
this.getList = this.getList.bind(this)
this.inputChang = this.inputChang.bind(this)
this.addItem = this.addItem.bind(this)
this.delItem = this.delItem.bind(this)
}
// react组件必不可少的方法,return 返回的是jsx的模板,可以理解为类似html+js的模板
render() {
return (
{/*在jsx中只能有一个根标签,使用<></>(幽灵标签)包裹,既能满足jsx的语法格式,在浏览器解析时不会解析幽灵标签,减少了dom树结构节点 */}
<>
<div>
<input value={this.state.inputValue} onChange={this.inputChang} /> <button onClick={this.addItem}>添加</button>
</div>
<ul>
{this.getList()}
</ul>
</>
);
}; // 遍历输出item
getList() {
...
}
// 动态改变输入框的值
inputChang( e ) {
...
} // 添加item
addItem() {
...
} // 删除item
delItem(index) {
...
}
}
// 导出TodoList
export default TodoList;
认识jsx
简单的jsx的语法
...
render() {
return (
{/*在jsx中只能有一个根标签,使用<></>(幽灵标签)包裹,既能满足jsx的语法格式,在浏览器解析时不会解析幽灵标签,减少了dom树结构节点 */}
<>
<div>
<input value={this.state.inputValue} onChange={this.inputChang} /> <button onClick={this.addItem}>添加</button>
</div>
<ul>
{this.getList()}
</ul>
</>
);
}; ...
在jsx中只能有一个根标签,使用<></>(幽灵标签)包裹,既能满足jsx的语法格式,在浏览器解析时不会解析幽灵标签,减少了dom树结构节点,也可以使用代码片段(Fragments ),效果和<></>相同,只是Fragments 还有更广泛的使用,后面会有详细说明
在jsx中使用注释,多行使用{/* 注释内容 */},单行使用
// 多行 {/* 多行注释内容 */}
{/*
多行注释内容
*/} // 单行
{
// 单行注释内容
}在jsx中使用组件的变量或者方法,使用{}包裹
在jsx中绑定的方法默认this指向undefined,所以需要使用bind绑定this的指向
// 方式1: 在constructor指定this
constructor(props){
super(props)
this.state = {
inputValue: "",
List: []
}
this.getList = this.getList.bind(this)
this.inputChang = this.inputChang.bind(this)
this.addItem = this.addItem.bind(this)
this.delItem = this.delItem.bind(this)
} // 方式2:绑定事件的时候指定this ...
<li onClick={this.delItem.bind(this,index)}></li>
...
组件的基本通讯
最简单的 父 ---> 子 传值
// 父组件通过在子组件标签上设置属性传递数据
<Boy
teacherName={ this.state.teacherName }
/> <Girl
teacherName={ this.state.teacherName }
/> // 子组件通过this.props获取父组件传递的数据
// this.props.teacherName 获取老师的名称
render(){
return (
<>
<p>
我是{this.state.boyName},我的老师是{this.props.teacherName},
我对老师很
<button onClick={()=> this.props.say(this.state.boyName,"满意") } >
满意
</button>
</p>
</>
)
}最简单的 子 ---> 父 通讯
// 父组件通过在子组件标签上设置属性传递数据
<Boy
say = { this.stuSay }
/> <Girl
say = { this.stuSay }
/>
// 子组件通过this.props获取父组件传递的数据
// this.props.say 获取父组件提供的方法,通过调用父组件的方法,将传递的数据作为参数传入,当父组件的方法被调用,就通过获取当前方法参数的方式,得到了子组件传递的数据
render(){
return (
<>
<p>
我是{this.state.boyName},
我对老师很
<button onClick={()=> this.props.say(this.state.boyName,"满意") } >
满意
</button>
</p>
</>
)
}最简单的非父子通讯
核心思路:找到共同的父组件,同时使用父组件传递的值和方法
过程有些复杂,这里省略了
teacher.js
import React,{ Component } from "react"
// 导入 Boy(男孩) 和 Girl(女孩) 组件
import Boy from "./boy"
import Girl from "./girl"
export default class Teacher extends Component {
constructor(props){
super(props)
this.state = {
teacherName: "Tom",
stuName: "",
stuSayContent: "",
boyName: "",
girlName: "",
boySayContent: "",
girlSayContent: ""
}
this.stuSay = this.stuSay.bind(this);
this.boySaySecret = this.boySaySecret.bind(this);
this.grilSaySecret = this.grilSaySecret.bind(this);
}
render(){
let evaluation = false
if (this.state.stuName!=="" && this.state.stuSayContent) {
evaluation = true
}
return (
<>
<p>我是{ this.state.teacherName }老师</p>
<div>
{
evaluation ? (<p>学生评价:{this.state.stuName}对我很{this.state.stuSayContent}</p>) : " "
}
</div>
<Boy
say = { this.stuSay }
teacherName={ this.state.teacherName }
boySaySecret = {this.boySaySecret}
girlSayContent = {this.state.girlSayContent}
/>
<Girl
say = { this.stuSay }
teacherName={ this.state.teacherName }
grilSaySecret = {this.grilSaySecret}
boySayContent = {this.state.boySayContent}
/>
</>
)
}
stuSay(stuName,stuSayContent){
this.setState(()=>{
return {
stuSayContent,
stuName
}
})
}
boySaySecret(constent){
this.setState(()=>{
return {
boySayContent : constent
}
})
}
grilSaySecret(constent){
this.setState(()=>{
return {
girlSayContent : constent
}
})
}
}
boy.js
import React,{ Component } from "react"
export default class Boy extends Component {
constructor(props){
super(props)
this.state = {
boyName: "龙震天"
}
}
render(){
return (
<>
<p>
我是{this.state.boyName},我的老师是{this.props.teacherName},
我对老师很
<button onClick={()=> this.props.say(this.state.boyName,"满意") } >
满意
</button>,
我想对女孩说:<button onClick={()=> this.props.boySaySecret("我喜欢你")}>悄悄话</button>,
她对我说:{this.props.girlSayContent}
</p>
</>
)
}
}
gril.js
import React,{ Component } from "react"
export default class Boy extends Component {
constructor(props){
super(props)
this.state = {
girlName: "怜香玉"
}
}
render(){
return (
<>
<p>
我是{this.state.girlName},我的老师是{this.props.teacherName},
我对老师很
<button onClick={()=> this.props.say(this.state.girlName,"不满意") } >
不满意
</button>,
我想对男孩说:<button onClick={() => this.props.grilSaySecret("我也是")}>悄悄话</button>,
他对我说:{this.props.boySayContent}
</p>
</>
)
}
}
高级部分
待续....
我的react学习的更多相关文章
- React学习笔记-1-什么是react,react环境搭建以及第一个react实例
什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...
- react学习小结(生命周期- 实例化时期 - 存在期- 销毁时期)
react学习小结 本文是我学习react的阶段性小结,如果看官你是react资深玩家,那么还请就此打住移步他处,如果你想给一些建议和指导,那么还请轻拍~ 目前团队内对react的使用非常普遍,之 ...
- React学习资料
以下是我整理的React学习资料,包括:React基础.Redux.reat-router, redux middleware, higher order components, React验证等, ...
- React学习笔记(一) 基础知识
现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我. React的基 ...
- React学习系列
React学习系列 系列学习react 翻译地址 https://scotch.io/tutorials/learning-react-getting-started-and-concepts 我是初 ...
- react学习笔记1--基础知识
什么是react A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES[React是一个用于构建用户界面的JavaScript库.] React之所以快, ...
- react 学习与使用记录
相关技术:webpack+react+react-router+redux+immutable 郭永峰react学习指南 1.git bash--windows命令行工具 --教程 下载地址 2. i ...
- 【JAVASCRIPT】React学习-JSX 语法
摘要 react 学习包括几个部分: 文本渲染 JSX 语法 组件化思想 数据流 JSX 语法 1. 定义 JSX 是javascript + xml 的合集,我们可以将javascript 与 ht ...
- 【JAVASCRIPT】React学习-如何构建一个组件
摘要 react 学习包括几个部分: 文本渲染 JSX 语法 组件化思想 数据流 组件化思想 组件就是 UI + UI 交互逻辑,组件有三个常规map , 分别为state 状态 . props 数据 ...
- 【JAVASCRIPT】React学习- 数据流(组件通信)
摘要 react 学习包括几个部分: 文本渲染 JSX 语法 组件化思想 数据流 一 组件通信如何实现 父子组件之间不存在继承关系 1.1 父=>子通信 父组件可以通过 this.refs.xx ...
随机推荐
- screen 状态为Attached 连不上
用 screen -ls, 显式当前状态为Attached, 但当前没有用户登陆些会话.screen此时正常状态应该为(Detached) 此时用screen -r ,怎么也登不上. 最后找到解决方 ...
- c# 从一个服务器 访问另外一个服务器上的文件
页面调用 function fnOpen(path) { window.open("~/FileHelp.ashx? url="); //window.open(url); } 后 ...
- 合理选择css3动画实现方式
使用css3实现动画,比js控制DOM属性的方式要高效很多.流畅很多,主要分transition和animation两大方式. transition适用于一次性变换 animation适用于循环动画和 ...
- Linux 系统的磁盘分区_【all】
磁盘的存储逻辑结构 1.主引导扇区(446+64+2) MBR(主引导记录)0磁头0磁道的第一扇区 446字节 -->存放系统的引导程序,同Windows 剩下的64字节,分区表(每个分区16字 ...
- Python学习---JSON补充内容[中文编码 + dumps解析]
JSON补充内容[微信解决中文乱码,接上] import json # 英文显示 dic = {"hello": "world"} str = json.dum ...
- Linux 系统必须掌握的文件_【all】
0.Linux 系统文件的详解 1.Linux 系统的网络配置文件 2.Linux 系统的DNS配置文件 3.Linux 系统的IP与域名解析文件[局域网的DNS] 4.Linux 系统的主机别名文件 ...
- Linxu系统修改文件描述符
修改系统文件描述符 文件描述符:无符号整数(0-65535),进程使用它来标示打开的文件 /etc/security/limits.conf:可以修改CPU,堆栈, 1.查看最大的标示符 u ...
- php5 Array 数组函数
函数 描述 array() 创建数组. array_change_key_case() 把数组中所有键更改为小写或大写. array_chunk() 把一个数组分割为新的数组块. array_colu ...
- HDU1407 测试你是否和LTC水平一样高
题目大意:给出一个num,计算方程x^2+y^2+z^2 = num的第一个正整数解(字典序),0 < num <= 10000. 方法参考了网上的博客,自己打了一波,发现还有很多不懂的地 ...
- using指令都用了这么多年了,其实还真没懂!
在C语言中,我们经常使用#include<stdio.h>指令来导入标准输入输出库,这确实很好理解,相当于把代码复制到当前的程序中. 但在C#语言中,当我们写Console程序时,经常在第 ...