01_hello_react
hello_react.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> hello_react</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
const VDOM = < h1> Hello, React< / h1>
ReactDOM. render ( VDOM , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
02_虚拟DOM的两种创建方式
1_使用jsx创建虚拟DOM.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 1_使用jsx创建虚拟DOM</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
const VDOM = (
< h1 id= "title" >
< span> Hello, React< / span>
< / h1>
)
ReactDOM. render ( VDOM , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
2_使用js创建虚拟DOM.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 2_使用js创建虚拟DOM</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" >
const VDOM = React. createElement ( 'h1' , { id : 'title' } , React. createElement ( 'span' , { } , 'Hello,React' ) )
ReactDOM. render ( VDOM , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
3_虚拟DOM与真实DOM.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 3_虚拟DOM与真实DOM</ title>
</ head>
< body>
< div id = " test" > </ div>
< div id = " demo" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
const VDOM = (
< h1 id= "title" >
< span> Hello, React< / span>
< / h1>
)
ReactDOM. render ( VDOM , document. getElementById ( 'test' ) )
const TDOM = document. getElementById ( 'demo' )
console. log ( '虚拟DOM' , VDOM ) ;
console. log ( '真实DOM' , TDOM ) ;
debugger ;
</ script>
</ body>
</ html>
03_jsx语法规则
jsx语法规则.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> jsx语法规则</ title>
< style>
.title {
background-color : orange;
width : 200px;
}
</ style>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
const myId = 'aTgUiGu'
const myData = 'HeLlo,rEaCt'
const VDOM = (
< div>
< h2 className= "title" id= { myId. toLowerCase ( ) } >
< span style= { { color : 'white' , fontSize : '29px' } } > { myData. toLowerCase ( ) } < / span>
< / h2>
< h2 className= "title" id= { myId. toUpperCase ( ) } >
< span style= { { color : 'white' , fontSize : '29px' } } > { myData. toLowerCase ( ) } < / span>
< / h2>
< input type= "text" / >
< / div>
)
ReactDOM. render ( VDOM , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
04_jsx的小练习
jsx小练习.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> jsx小练习</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
const data = [ 'Angular' , 'React' , 'Vue' ]
const VDOM = (
< div>
< h1> 前端js框架列表< / h1>
< ul>
{
data. map ( ( item, index ) => {
return < li key= { index} > { item} < / li>
} )
}
< / ul>
< / div>
)
ReactDOM. render ( VDOM , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
05_react中定义组件
1_函数式组件.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 1_函数式组件</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
function MyComponent ( ) {
console. log ( this ) ;
return < h2> 我是用函数定义的组件 ( 适用于【简单组件】的定义) < / h2>
}
ReactDOM. render ( < MyComponent/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
2_类式组件.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 2_类式组件</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class MyComponent extends React. Component {
render ( ) {
console. log ( 'render中的this:' , this ) ;
return < h2> 我是用类定义的组件 ( 适用于【复杂组件】的定义) < / h2>
}
}
ReactDOM. render ( < MyComponent/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
06_组件实例三大属性1_state
1_state.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> state</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Weather extends React. Component {
constructor ( props ) {
console. log ( 'constructor' ) ;
super ( props)
this . state = { isHot : false , wind : '微风' }
this . changeWeather = this . changeWeather . bind ( this )
}
render ( ) {
console. log ( 'render' ) ;
const { isHot, wind} = this . state
return < h1 onClick= { this . changeWeather} > 今天天气很{ isHot ? '炎热' : '凉爽' } ,{ wind} < / h1>
}
changeWeather ( ) {
console. log ( 'changeWeather' ) ;
const isHot = this . state. isHot
this . setState ( { isHot : ! isHot} )
console. log ( this ) ;
}
}
ReactDOM. render ( < Weather/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
2_state的简写方式.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> state简写方式</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Weather extends React. Component {
state = { isHot : false , wind : '微风' }
render ( ) {
const { isHot, wind} = this . state
return < h1 onClick= { this . changeWeather} > 今天天气很{ isHot ? '炎热' : '凉爽' } ,{ wind} < / h1>
}
changeWeather = ( ) => {
const isHot = this . state. isHot
this . setState ( { isHot : ! isHot} )
}
}
ReactDOM. render ( < Weather/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
07_组件实例三大属性2_props
1_props基本使用.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> props基本使用</ title>
</ head>
< body>
< div id = " test1" > </ div>
< div id = " test2" > </ div>
< div id = " test3" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Person extends React. Component {
render ( ) {
const { name, age, sex} = this . props
return (
< ul>
< li> 姓名:{ name} < / li>
< li> 性别:{ sex} < / li>
< li> 年龄:{ age+ 1 } < / li>
< / ul>
)
}
}
ReactDOM. render ( < Person name= "jerry" age= { 19 } sex= "男" / > , document. getElementById ( 'test1' ) )
ReactDOM. render ( < Person name= "tom" age= { 18 } sex= "女" / > , document. getElementById ( 'test2' ) )
const p = { name : '老刘' , age : 18 , sex : '女' }
ReactDOM. render ( < Person { ... p} / > , document. getElementById ( 'test3' ) )
</ script>
</ body>
</ html>
2_对props进行限制.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 对props进行限制</ title>
</ head>
< body>
< div id = " test1" > </ div>
< div id = " test2" > </ div>
< div id = " test3" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/javascript" src = " ../js/prop-types.js" > </ script>
< script type = " text/babel" >
class Person extends React. Component {
render ( ) {
const { name, age, sex} = this . props
return (
< ul>
< li> 姓名:{ name} < / li>
< li> 性别:{ sex} < / li>
< li> 年龄:{ age+ 1 } < / li>
< / ul>
)
}
}
Person. propTypes = {
name : PropTypes. string. isRequired,
sex : PropTypes. string,
age : PropTypes. number,
speak : PropTypes. func,
}
Person. defaultProps = {
sex : '男' ,
age : 18
}
ReactDOM. render ( < Person name= { 100 } speak= { speak} / > , document. getElementById ( 'test1' ) )
ReactDOM. render ( < Person name= "tom" age= { 18 } sex= "女" / > , document. getElementById ( 'test2' ) )
const p = { name : '老刘' , age : 18 , sex : '女' }
ReactDOM. render ( < Person { ... p} / > , document. getElementById ( 'test3' ) )
function speak ( ) {
console. log ( '我说话了' ) ;
}
</ script>
</ body>
</ html>
3_props的简写方式.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 对props进行限制</ title>
</ head>
< body>
< div id = " test1" > </ div>
< div id = " test2" > </ div>
< div id = " test3" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/javascript" src = " ../js/prop-types.js" > </ script>
< script type = " text/babel" >
class Person extends React. Component {
constructor ( props ) {
super ( props)
console. log ( 'constructor' , this . props) ;
}
static propTypes = {
name : PropTypes. string. isRequired,
sex : PropTypes. string,
age : PropTypes. number,
}
static defaultProps = {
sex : '男' ,
age : 18
}
render ( ) {
const { name, age, sex} = this . props
return (
< ul>
< li> 姓名:{ name} < / li>
< li> 性别:{ sex} < / li>
< li> 年龄:{ age+ 1 } < / li>
< / ul>
)
}
}
ReactDOM. render ( < Person name= "jerry" / > , document. getElementById ( 'test1' ) )
</ script>
</ body>
</ html>
4_函数组件使用props.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 对props进行限制</ title>
</ head>
< body>
< div id = " test1" > </ div>
< div id = " test2" > </ div>
< div id = " test3" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/javascript" src = " ../js/prop-types.js" > </ script>
< script type = " text/babel" >
function Person ( props ) {
const { name, age, sex} = props
return (
< ul>
< li> 姓名:{ name} < / li>
< li> 性别:{ sex} < / li>
< li> 年龄:{ age} < / li>
< / ul>
)
}
Person. propTypes = {
name : PropTypes. string. isRequired,
sex : PropTypes. string,
age : PropTypes. number,
}
Person. defaultProps = {
sex : '男' ,
age : 18
}
ReactDOM. render ( < Person name= "jerry" / > , document. getElementById ( 'test1' ) )
</ script>
</ body>
</ html>
08_组件实例三大属性3_refs
1_字符串形式的ref.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 1_字符串形式的ref</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Demo extends React. Component {
showData = ( ) => {
const { input1} = this . refs
alert ( input1. value)
}
showData2 = ( ) => {
const { input2} = this . refs
alert ( input2. value)
}
render ( ) {
return (
< div>
< input ref= "input1" type= "text" placeholder= "点击按钮提示数据" / > & nbsp;
< button onClick= { this . showData} > 点我提示左侧的数据< / button> & nbsp;
< input ref= "input2" onBlur= { this . showData2} type= "text" placeholder= "失去焦点提示数据" / >
< / div>
)
}
}
ReactDOM. render ( < Demo a= "1" b= "2" / > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
2_回调函数形式的ref.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 1_字符串形式的ref</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Demo extends React. Component {
showData = ( ) => {
const { input1} = this
alert ( input1. value)
}
showData2 = ( ) => {
const { input2} = this
alert ( input2. value)
}
render ( ) {
return (
< div>
< input ref= { c => this . input1 = c } type= "text" placeholder= "点击按钮提示数据" / > & nbsp;
< button onClick= { this . showData} > 点我提示左侧的数据< / button> & nbsp;
< input onBlur= { this . showData2} ref= { c => this . input2 = c } type= "text" placeholder= "失去焦点提示数据" / > & nbsp;
< / div>
)
}
}
ReactDOM. render ( < Demo a= "1" b= "2" / > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
3_回调ref中回调执行次数的问题.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 3_回调ref中回调执行次数的问题</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Demo extends React. Component {
state = { isHot : false }
showInfo = ( ) => {
const { input1} = this
alert ( input1. value)
}
changeWeather = ( ) => {
const { isHot} = this . state
this . setState ( { isHot : ! isHot} )
}
saveInput = ( c ) => {
this . input1 = c;
console. log ( '@' , c) ;
}
render ( ) {
const { isHot} = this . state
return (
< div>
< h2> 今天天气很{ isHot ? '炎热' : '凉爽' } < / h2>
{ }
< input ref= { this . saveInput} type= "text" / > < br/ > < br/ >
< button onClick= { this . showInfo} > 点我提示输入的数据< / button>
< button onClick= { this . changeWeather} > 点我切换天气< / button>
< / div>
)
}
}
ReactDOM. render ( < Demo/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
4_createRef的使用.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 4_createRef</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Demo extends React. Component {
myRef = React. createRef ( )
myRef2 = React. createRef ( )
showData = ( ) => {
alert ( this . myRef. current. value) ;
}
showData2 = ( ) => {
alert ( this . myRef2. current. value) ;
}
render ( ) {
return (
< div>
< input ref= { this . myRef} type= "text" placeholder= "点击按钮提示数据" / > & nbsp;
< button onClick= { this . showData} > 点我提示左侧的数据< / button> & nbsp;
< input onBlur= { this . showData2} ref= { this . myRef2} type= "text" placeholder= "失去焦点提示数据" / > & nbsp;
< / div>
)
}
}
ReactDOM. render ( < Demo a= "1" b= "2" / > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
09_react中的事件处理
事件处理.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 事件处理</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Demo extends React. Component {
myRef = React. createRef ( )
myRef2 = React. createRef ( )
showData = ( event ) => {
console. log ( event. target) ;
alert ( this . myRef. current. value) ;
}
showData2 = ( event ) => {
alert ( event. target. value) ;
}
render ( ) {
return (
< div>
< input ref= { this . myRef} type= "text" placeholder= "点击按钮提示数据" / > & nbsp;
< button onClick= { this . showData} > 点我提示左侧的数据< / button> & nbsp;
< input onBlur= { this . showData2} type= "text" placeholder= "失去焦点提示数据" / > & nbsp;
< / div>
)
}
}
ReactDOM. render ( < Demo a= "1" b= "2" / > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
10_react中收集表单数据
1_非受控组件.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 1_非受控组件</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Login extends React. Component {
handleSubmit = ( event ) => {
event. preventDefault ( )
const { username, password} = this
alert ( ` 你输入的用户名是: ${ username. value} ,你输入的密码是: ${ password. value} ` )
}
render ( ) {
return (
< form onSubmit= { this . handleSubmit} >
用户名:< input ref= { c => this . username = c} type= "text" name= "username" / >
密码:< input ref= { c => this . password = c} type= "password" name= "password" / >
< button> 登录< / button>
< / form>
)
}
}
ReactDOM. render ( < Login/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
2_受控组件.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 2_受控组件</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Login extends React. Component {
state = {
username : '' ,
password : ''
}
saveUsername = ( event ) => {
this . setState ( { username : event. target. value} )
}
savePassword = ( event ) => {
this . setState ( { password : event. target. value} )
}
handleSubmit = ( event ) => {
event. preventDefault ( )
const { username, password} = this . state
alert ( ` 你输入的用户名是: ${ username} ,你输入的密码是: ${ password} ` )
}
render ( ) {
return (
< form onSubmit= { this . handleSubmit} >
用户名:< input onChange= { this . saveUsername} type= "text" name= "username" / >
密码:< input onChange= { this . savePassword} type= "password" name= "password" / >
< button> 登录< / button>
< / form>
)
}
}
ReactDOM. render ( < Login/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
11_高阶函数_函数柯里化
1_高阶函数_函数柯里化.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 高阶函数_函数柯里化</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Login extends React. Component {
state = {
username : '' ,
password : ''
}
saveFormData = ( dataType ) => {
return ( event ) => {
this . setState ( { [ dataType] : event. target. value} )
}
}
handleSubmit = ( event ) => {
event. preventDefault ( )
const { username, password} = this . state
alert ( ` 你输入的用户名是: ${ username} ,你输入的密码是: ${ password} ` )
}
render ( ) {
return (
< form onSubmit= { this . handleSubmit} >
用户名:< input onChange= { this . saveFormData ( 'username' ) } type= "text" name= "username" / >
密码:< input onChange= { this . saveFormData ( 'password' ) } type= "password" name= "password" / >
< button> 登录< / button>
< / form>
)
}
}
ReactDOM. render ( < Login/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
2_不用函数柯里化的实现.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 2_不用函数柯里化的实现</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Login extends React. Component {
state = {
username : '' ,
password : ''
}
saveFormData = ( dataType, event ) => {
this . setState ( { [ dataType] : event. target. value} )
}
handleSubmit = ( event ) => {
event. preventDefault ( )
const { username, password} = this . state
alert ( ` 你输入的用户名是: ${ username} ,你输入的密码是: ${ password} ` )
}
render ( ) {
return (
< form onSubmit= { this . handleSubmit} >
用户名:< input onChange= { event => this . saveFormData ( 'username' , event) } type= "text" name= "username" / >
密码:< input onChange= { event => this . saveFormData ( 'password' , event) } type= "password" name= "password" / >
< button> 登录< / button>
< / form>
)
}
}
ReactDOM. render ( < Login/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
12_组件的生命周期
1_引出生命周期.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 1_引出生命周期</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Life extends React. Component {
state = { opacity : 1 }
death = ( ) => {
ReactDOM. unmountComponentAtNode ( document. getElementById ( 'test' ) )
}
componentDidMount ( ) {
console. log ( 'componentDidMount' ) ;
this . timer = setInterval ( ( ) => {
let { opacity} = this . state
opacity -= 0.1
if ( opacity <= 0 ) opacity = 1
this . setState ( { opacity} )
} , 200 ) ;
}
componentWillUnmount ( ) {
clearInterval ( this . timer)
}
render ( ) {
console. log ( 'render' ) ;
return (
< div>
< h2 style= { { opacity : this . state. opacity} } > React学不会怎么办?< / h2>
< button onClick= { this . death} > 不活了< / button>
< / div>
)
}
}
ReactDOM. render ( < Life/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
2_react生命周期(旧).html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 2_react生命周期(旧)</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Count extends React. Component {
constructor ( props ) {
console. log ( 'Count---constructor' ) ;
super ( props)
this . state = { count : 0 }
}
add = ( ) => {
const { count} = this . state
this . setState ( { count : count+ 1 } )
}
death = ( ) => {
ReactDOM. unmountComponentAtNode ( document. getElementById ( 'test' ) )
}
force = ( ) => {
this . forceUpdate ( )
}
componentWillMount ( ) {
console. log ( 'Count---componentWillMount' ) ;
}
componentDidMount ( ) {
console. log ( 'Count---componentDidMount' ) ;
}
componentWillUnmount ( ) {
console. log ( 'Count---componentWillUnmount' ) ;
}
shouldComponentUpdate ( ) {
console. log ( 'Count---shouldComponentUpdate' ) ;
return true
}
componentWillUpdate ( ) {
console. log ( 'Count---componentWillUpdate' ) ;
}
componentDidUpdate ( ) {
console. log ( 'Count---componentDidUpdate' ) ;
}
render ( ) {
console. log ( 'Count---render' ) ;
const { count} = this . state
return (
< div>
< h2> 当前求和为:{ count} < / h2>
< button onClick= { this . add} > 点我+ 1 < / button>
< button onClick= { this . death} > 卸载组件< / button>
< button onClick= { this . force} > 不更改任何状态中的数据,强制更新一下< / button>
< / div>
)
}
}
class A extends React. Component {
state = { carName : '奔驰' }
changeCar = ( ) => {
this . setState ( { carName : '奥拓' } )
}
render ( ) {
return (
< div>
< div> 我是A 组件< / div>
< button onClick= { this . changeCar} > 换车< / button>
< B carName= { this . state. carName} / >
< / div>
)
}
}
class B extends React. Component {
componentWillReceiveProps ( props ) {
console. log ( 'B---componentWillReceiveProps' , props) ;
}
shouldComponentUpdate ( ) {
console. log ( 'B---shouldComponentUpdate' ) ;
return true
}
componentWillUpdate ( ) {
console. log ( 'B---componentWillUpdate' ) ;
}
componentDidUpdate ( ) {
console. log ( 'B---componentDidUpdate' ) ;
}
render ( ) {
console. log ( 'B---render' ) ;
return (
< div> 我是B 组件,接收到的车是 : { this . props. carName} < / div>
)
}
}
ReactDOM. render ( < Count/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
3_react生命周期(新).html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 3_react生命周期(新)</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/17.0.1/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/17.0.1/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/17.0.1/babel.min.js" > </ script>
< script type = " text/babel" >
class Count extends React. Component {
constructor ( props ) {
console. log ( 'Count---constructor' ) ;
super ( props)
this . state = { count : 0 }
}
add = ( ) => {
const { count} = this . state
this . setState ( { count : count+ 1 } )
}
death = ( ) => {
ReactDOM. unmountComponentAtNode ( document. getElementById ( 'test' ) )
}
force = ( ) => {
this . forceUpdate ( )
}
static getDerivedStateFromProps ( props, state ) {
console. log ( 'getDerivedStateFromProps' , props, state) ;
return null
}
getSnapshotBeforeUpdate ( ) {
console. log ( 'getSnapshotBeforeUpdate' ) ;
return 'atguigu'
}
componentDidMount ( ) {
console. log ( 'Count---componentDidMount' ) ;
}
componentWillUnmount ( ) {
console. log ( 'Count---componentWillUnmount' ) ;
}
shouldComponentUpdate ( ) {
console. log ( 'Count---shouldComponentUpdate' ) ;
return true
}
componentDidUpdate ( preProps, preState, snapshotValue ) {
console. log ( 'Count---componentDidUpdate' , preProps, preState, snapshotValue) ;
}
render ( ) {
console. log ( 'Count---render' ) ;
const { count} = this . state
return (
< div>
< h2> 当前求和为:{ count} < / h2>
< button onClick= { this . add} > 点我+ 1 < / button>
< button onClick= { this . death} > 卸载组件< / button>
< button onClick= { this . force} > 不更改任何状态中的数据,强制更新一下< / button>
< / div>
)
}
}
ReactDOM. render ( < Count count= { 199 } / > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
4_getSnapshotBeforeUpdate的使用场景.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 4_getSnapShotBeforeUpdate的使用场景</ title>
< style>
.list {
width : 200px;
height : 150px;
background-color : skyblue;
overflow : auto;
}
.news {
height : 30px;
}
</ style>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/17.0.1/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/17.0.1/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/17.0.1/babel.min.js" > </ script>
< script type = " text/babel" >
class NewsList extends React. Component {
state = { newsArr : [ ] }
componentDidMount ( ) {
setInterval ( ( ) => {
const { newsArr} = this . state
const news = '新闻' + ( newsArr. length+ 1 )
this . setState ( { newsArr : [ news, ... newsArr] } )
} , 1000 ) ;
}
getSnapshotBeforeUpdate ( ) {
return this . refs. list. scrollHeight
}
componentDidUpdate ( preProps, preState, height ) {
this . refs. list. scrollTop += this . refs. list. scrollHeight - height
}
render ( ) {
return (
< div className= "list" ref= "list" >
{
this . state. newsArr. map ( ( n, index ) => {
return < div key= { index} className= "news" > { n} < / div>
} )
}
< / div>
)
}
}
ReactDOM. render ( < NewsList/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
13_DOM的Diffing算法
1_验证Diffing算法.html
<! DOCTYPE html >
< html>
< head>
< meta charset = " UTF-8" >
< title> 验证diff算法</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/17.0.1/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/17.0.1/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/17.0.1/babel.min.js" > </ script>
< script type = " text/babel" >
class Time extends React. Component {
state = { date : new Date ( ) }
componentDidMount ( ) {
setInterval ( ( ) => {
this . setState ( {
date : new Date ( )
} )
} , 1000 )
}
render ( ) {
return (
< div>
< h1> hello< / h1>
< input type= "text" / >
< span>
现在是:{ this . state. date. toTimeString ( ) }
< input type= "text" / >
< / span>
< / div>
)
}
}
ReactDOM. render ( < Time/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
2_key的作用.html
<! DOCTYPE html >
< html>
< head>
< meta charset = " UTF-8" >
< title> key的作用</ title>
</ head>
< body>
< div id = " test" > </ div>
< script type = " text/javascript" src = " ../js/react.development.js" > </ script>
< script type = " text/javascript" src = " ../js/react-dom.development.js" > </ script>
< script type = " text/javascript" src = " ../js/babel.min.js" > </ script>
< script type = " text/babel" >
class Person extends React. Component {
state = {
persons : [
{ id : 1 , name : '小张' , age : 18 } ,
{ id : 2 , name : '小李' , age : 19 } ,
]
}
add = ( ) => {
const { persons} = this . state
const p = { id : persons. length+ 1 , name : '小王' , age : 20 }
this . setState ( { persons : [ p, ... persons] } )
}
render ( ) {
return (
< div>
< h2> 展示人员信息< / h2>
< button onClick= { this . add} > 添加一个小王< / button>
< h3> 使用index(索引值)作为key< / h3>
< ul>
{
this . state. persons. map ( ( personObj, index ) => {
return < li key= { index} > { personObj. name} -- - { personObj. age} < input type= "text" / > < / li>
} )
}
< / ul>
< hr/ >
< hr/ >
< h3> 使用id(数据的唯一标识)作为key< / h3>
< ul>
{
this . state. persons. map ( ( personObj ) => {
return < li key= { personObj. id} > { personObj. name} -- - { personObj. age} < input type= "text" / > < / li>
} )
}
< / ul>
< / div>
)
}
}
ReactDOM. render ( < Person/ > , document. getElementById ( 'test' ) )
</ script>
</ body>
</ html>
复习
1_类的基本知识.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 1_类的基本知识</ title>
</ head>
< body>
< script type = " text/javascript" >
class Person {
constructor ( name, age ) {
this . name = name
this . age = age
}
speak ( ) {
console. log ( ` 我叫 ${ this . name} ,我年龄是 ${ this . age} ` ) ;
}
}
class Student extends Person {
constructor ( name, age, grade ) {
super ( name, age)
this . grade = grade
this . school = '尚硅谷'
}
speak ( ) {
console. log ( ` 我叫 ${ this . name} ,我年龄是 ${ this . age} ,我读的是 ${ this . grade} 年级 ` ) ;
this . study ( )
}
study ( ) {
console. log ( '我很努力的学习' ) ;
}
}
class Car {
constructor ( name, price ) {
this . name = name
this . price = price
}
a = 1
wheel = 4
static demo = 100
}
const c1 = new Car ( '奔驰c63' , 199 )
console. log ( c1) ;
console. log ( Car. demo) ;
</ script>
</ body>
</ html>
2_原生事件绑定.html
<! DOCTYPE html >
< html>
< head>
< meta charset = " UTF-8" />
< title> Document</ title>
</ head>
< body>
< button id = " btn1" > 按钮1</ button>
< button id = " btn2" > 按钮2</ button>
< button onclick = " demo ( ) " > 按钮3</ button>
< script type = " text/javascript" >
const btn1 = document. getElementById ( 'btn1' )
btn1. addEventListener ( 'click' , ( ) => {
alert ( '按钮1被点击了' )
} )
const btn2 = document. getElementById ( 'btn2' )
btn2. onclick = ( ) => {
alert ( '按钮2被点击了' )
}
function demo ( ) {
alert ( '按钮3被点击了' )
}
</ script>
</ body>
</ html>
3_类方法中的this指向.html
<! DOCTYPE html >
< html>
< head>
< meta charset = " UTF-8" />
< title> Document</ title>
</ head>
< body>
< script type = " text/javascript" >
class Person {
constructor ( name, age ) {
this . name = name
this . age = age
}
study ( ) {
console. log ( this ) ;
}
}
const p1 = new Person ( 'tom' , 18 )
p1. study ( )
const x = p1. study
x ( )
</ script>
</ body>
</ html>
4_展开运算符.html
<! DOCTYPE html >
< html>
< head>
< meta charset = " UTF-8" />
< title> Document</ title>
</ head>
< body>
< script type = " text/javascript" >
let arr1 = [ 1 , 3 , 5 , 7 , 9 ]
let arr2 = [ 2 , 4 , 6 , 8 , 10 ]
console. log ( ... arr1) ;
let arr3 = [ ... arr1, ... arr2]
function sum ( ... numbers) {
return numbers. reduce ( ( preValue, currentValue ) => {
return preValue + currentValue
} )
}
console. log ( sum ( 1 , 2 , 3 , 4 ) ) ;
let person = { name : 'tom' , age : 18 }
let person2 = { ... person}
person. name = 'jerry'
console. log ( person2) ;
console. log ( person) ;
let person3 = { ... person, name : 'jack' , address : "地球" }
console. log ( person3) ;
</ script>
</ body>
</ html>
5_对象相关的知识.html
<! DOCTYPE html >
< html>
< head>
< meta charset = " UTF-8" />
< title> Document</ title>
</ head>
< body>
< script type = " text/javascript" >
let a = 'name'
let obj = { }
obj[ a] = 'tom'
console. log ( obj) ;
</ script>
</ body>
</ html>
6_演示函数的柯里化.html
<! DOCTYPE html >
< html>
< head>
< meta charset = " UTF-8" />
< title> Document</ title>
</ head>
< body>
< script type = " text/javascript" >
function sum ( a ) {
return ( b ) => {
return ( c ) => {
return a+ b+ c
}
}
}
const result = sum ( 1 ) ( 2 ) ( 3 )
console. log ( result) ;
</ script>
</ body>
</ html>