react组件之非受控组件和高阶组件
非受控组件
受控组件和非受控组件的区别 ----- value
import React, { Component } from 'react'
export default class extends Component {
constructor (props) {
super(props);
this.state = {
username: '吴大勋',
password: '123465'
}
}
render () {
return (
<div>
<h1>非受控组件</h1>
<input type="text" defaultValue = { this.state.username} ref="username" onChange={ () => {
if(this.refs.username.value.length < 6) {
console.log('111111111')
} else {
console.log('ok')
}
this.setState({
username: this.refs.username.value
})
}}/>
<input type="password" defaultValue = { this.state.password} ref="password" onChange={ () => {
this.setState({
password: this.refs.password.value
})
}}/>
<input type="file" ref="file" multiple/>
<button onClick= { () => {
console.log(this.refs.username.value)
console.log(this.refs.password.value)
// .files可以获取文件的信息
/**
* lastModified: 1572398893933
lastModifiedDate: Wed Oct 30 2019 09:28:13 GMT+0800 (中国标准时间) {}
name: "2.png"
size: 24396
type: "image/png"
webkitRelativePath: ""
*/
console.log(this.refs.file.files)
} }>获取表单信息</button>
</div>
)
}
}
高阶组件
高阶组件其实就是一个纯函数,接收一个组件作为参数,返回一个新的组件
高阶组件1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body> </body>
<script src="test.js"></script>
<script> </script>
</html>
function welcome(){
}
function goodbye(){
}
// 高阶函数
function func (fn) { // fn为一个参数
return function () {
let username = localStorage.getItem('username');
console.log(username)
}
}
welcomeFn = func(welcome)
goodbyeFn = func(goodbye)
// console.log(welcomeFn)
goodbyeFn()
goodbyeFn()
高阶组件2
import React, { Component } from 'react'
import Welcome from './Welcome'
import Goodbye from './Goodbye'
export default class extends Component {
constructor (props) {
super(props);
}
render () {
return (
<div>
<h1>高阶组件</h1>
<Welcome />
<Goodbye />
</div>
)
}
}
welcome.js
import React, {Component} from 'react'
class Com extends Component {
constructor (props) {
super(props)
this.state = {
username: ''
}
}
componentDidMount () {
let username = localStorage.getItem('username')
this.setState({
username
})
}
render () {
return (
<div>
welcome { this.state.username }
</div>
)
}
}
export default Com
goodbye.js
import React, {Component} from 'react'
class Com extends Component {
constructor (props) {
super(props)
this.state = {
username: ''
}
}
componentDidMount () {
let username = localStorage.getItem('username')
this.setState({
username
})
}
render () {
return (
<div>
goodbye { this.state.username }
</div>
)
}
}
export default Com
简化高阶组件3
import React, { Component } from 'react'
import Welcome from './Welcome'
import Goodbye from './Goodbye'
export default class extends Component {
constructor (props) {
super(props);
}
render () {
return (
<div>
<h1>高阶组件</h1>
<Welcome />
<Goodbye />
</div>
)
}
}
welcome.js
import React, {Component} from 'react'
import higherorderfn from './higherorderfn'
// console.log(higherorderfn)
class Com extends Component {
render () {
return (
<div>
welcome ---- { this.props.username }
</div>
)
}
}
const NewCom = higherorderfn(Com)
export default NewCom
goodbye.js
import React, {Component} from 'react'
import higherorderfn from './higherorderfn'
// console.log(higherorderfn)
class Com extends Component {
render () {
return (
<div>
goodbye --- { this.props.username }
</div>
)
}
}
const NewCom = higherorderfn(Com)
export default NewCom
higherorderfn.js
import React, {Component} from 'react'
import higherorderfn from './higherorderfn'
// console.log(higherorderfn)
class Com extends Component {
render () {
return (
<div>
goodbye --- { this.props.username }
</div>
)
}
}
const NewCom = higherorderfn(Com)
export default NewCom
react组件之非受控组件和高阶组件的更多相关文章
- React 精要面试题讲解(五) 高阶组件真解
说明与目录 在学习本章内容之前,最好是具备react中'插槽(children)'及'组合与继承' 这两点的知识积累. 详情请参照React 精要面试题讲解(四) 组合与继承不得不说的秘密. 哦不好意 ...
- React躬行记(10)——高阶组件
高阶组件(High Order Component,简称HOC)不是一个真的组件,而是一个没有副作用的纯函数,以组件作为参数,返回一个功能增强的新组件,在很多第三方库(例如Redux.Relay等)中 ...
- React组件重构:嵌套+继承 与 高阶组件
前言 在最近做的一个react项目中,遇到了一个比较典型的需要重构的场景:提取两个组件中共同的部分. 最开始通过使用嵌套组件和继承的方式完成了这次重构. 但是后来又用高阶组件重新写了一遍,发现更好一点 ...
- react.js 高阶组件----很简单的实例理解高阶组件思想
调试代码之前,我设置了两个缓存 分别是username和content 在控制台console设置两个缓存代码 localStorage.setItem('username','老王')localSt ...
- 聊聊React高阶组件(Higher-Order Components)
使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低 ...
- React文档(二十四)高阶组件
高阶组件(HOC)是React里的高级技术为了应对重用组件的逻辑.HOCs本质上不是React API的一部分.它是从React的组合性质中显露出来的模式. 具体来说,一个高阶组件就是一个获取一个组件 ...
- 函数式编程与React高阶组件
相信不少看过一些框架或者是类库的人都有印象,一个函数叫什么creator或者是什么什么createToFuntion,总是接收一个函数,来返回另一个函数.这是一个高阶函数,它可以接收函数可以当参数,也 ...
- React 高阶组件浅析
高阶组件的这种写法的诞生来自于社区的实践,目的是解决一些交叉问题(Cross-Cutting Concerns).而最早时候 React 官方给出的解决方案是使用 mixin .而 React 也在官 ...
- react 高阶组件的 理解和应用
高阶组件是什么东西 简单的理解是:一个包装了另一个基础组件的组件.(相对高阶组件来说,我习惯把被包装的组件称为基础组件) 注意:这里说的是包装,可以理解成包裹和组装: 具体的是高阶组件的两种形式吧: ...
- react高阶组件
高阶组件 为了提高组件复用性,在react中就有了HOC(Higher-Order Component)的概念.所谓的高阶组件,其本质依旧是组件,只是它返回另外一个组件,产生新的组件可以对属性进行包装 ...
随机推荐
- sequelize的创建接口以及模糊查询
第一步: 在routes里面复制index.js更改为xxx.js(例如arctile.js) res.json()返回的就是json文件 第二步: 在views里app.js引用路由 第三步:查找数 ...
- 身为一个java开发人员对目前java开发的一些小小的认识
为什么要学java? 第一要工作,第二想要高薪 首先身为java开发工作人员你的思想认知程度要搞,思想认知程度决定你的专业技能水平 JavaSE工程师 B/S 架构 ---- 浏览器 ...
- Unity中实现在规定时间内从一个值递增到另一个值
1.进度条(在规定时间内完成进度条) private Image progressBar; private float currentProgress = 0; /// <summary> ...
- 导出接口 生成doc文档
public function test1(){ echo ' <html xmlns:o="urn:schemas-microsoft-com:office:office" ...
- openEuler22.09初始化脚本
#!/bin/bash s=`nmcli d | grep "已断开" | awk '{print $1}'` echo /etc/sysconfig/network-script ...
- 如何在微信小程序中使用ECharts图表
在微信小程序中使用ECharts 1. 下载插件 首先,下载 GitHub 上的 ecomfe/echarts-for-weixin 项目. 下载链接:ecomfe/echarts-for-weixi ...
- 前端上传获取excel文件后,如何读取excel文件的内容
1.安装xlsx npm install xlsx --save-dev 2.引入xlsx并封装读取excel方法 import * as XLSX from "xlsx"; /* ...
- mysql索引的面试常问问题
- 在LaTeX中使用BibTeX时的一个问题及其解决:编译PDF不随bib文件更新
问题:更新bib文件之后,编译tex文件输出的PDF文件没有相应的更新. 原理: bbl文件才是引文的信息,bib文件只是用于生成bbl文件的一个"数据集"而已. 一般来说,LaT ...
- power shell 删除应用
public static UwpAppInfo SearchUwpAppByName(string appName) { UwpAppInfo app = null; try { string re ...