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)的概念.所谓的高阶组件,其本质依旧是组件,只是它返回另外一个组件,产生新的组件可以对属性进行包装 ...
随机推荐
- ORA-01427: 单行子查询返回多个行 出现原因及对应防止措施
原因:没有做好子查询约束条件 解决方法:1.加 AND ROWNUM =1 条件 但筛选结果可能并非一定是你想要的 2.匹配查询唯一约束条件
- js 导出excle文件(多页)
--前提:页面生成相应的表格数据 例如: <table id="a"> <tr> <th></th> </tr> ...
- #科技 #资讯 #生活 RTX 4080显卡在欧跌破建议零售价, 小鹏回应自研电池, Meta因泄漏超5亿用户资料被罚款 ,黑猫投诉开启2022“你我同心 反诈同行”系列活动,这就是今天的其它大新闻
今天是2022年11月29日 十一月初六 现在是下午15:29 下面是今天的其他大新闻 #NEWS 1 #RTX 4080在欧跌破建议零售价 ( IT 之家 )据 PCGamesHardware 报道 ...
- Jmeter二、开始使用
一.最简单的性能测试脚本 testplan→ thread group→HTTP request→view results tree.jmx后缀文件,xml文件校验 二.使用过程中其他需要注意的 1. ...
- 网易二面-Arthas的底层原理
众所周知,阿里开源的Arthas已经成为Java开发中调优的基本工具,其功能在于监控JVM运行情况,并对CPU.内存状况生成报告或者是火炬图. 从JDK5开始,java.lang.instrument ...
- visio任意图形填充
开发工具->操作->修剪->按住Shift键点击填充图形所有边 同时选中后->组合->开发工具->操作->连接 回到开始选项卡选择填充颜色 中途不要点击其他, ...
- C Ⅸ
数组例子:统计个数 #include <stdio.h> int main(void) { int x; int count[10]; int i; ...
- celery工作原理和代码
1.celery介绍 pip install celery == 4.4.7 pip install redis == 3.5.3 pip install eventlet == 0.26.1 Cel ...
- JUC学习!
JUC 1.what? JUC就是java.util.concurrent下面的类包,专门用于多线程的开发. 2.why? 解决多线程.高并发 3.how? || || ﹀ point1:vola ...
- tuxedo How To Disable Server Side Trace Which IS Enabled BY Client?
影响版本: Oracle Tuxedo - Version 8.1 to 11.1.1.2.0Information in this document applies to any platform. ...