本人的React学习笔记分类(也是对应本人技术成长过程):[想快速入门看这部分]、[想对React系统全面进行学习的同学看这里]、[对基础学习完成且有了一定开发经验,想尝试解析源码的看这里]

一、生命周期

生命周期即是组件从实例化到渲染到最终的从页面销毁,整个过程就是生命周期,在这个生命周期中,我们有许多可以调用的事件,也俗称为钩子函数

1、生命周期的三个状态:

  1. Mounting:将组件插入到DOM中
  2. Updating:将数据更新到DOM中
  3. Unmounting:将组件移除DOM中

2、生命周期中的钩子函数(方法、事件)

CompontWillMount :组件将要渲染,AJAX,添加动画前的类

CompontDidMount:组件渲染完毕,添加动画

componentWillReceiveProps:组件将要接受props数据,查看接收props的数据什么

ShouldComponentUpdate:组件接收到新的state或者props,判断是否更新。返回布尔值

CompontWillUpdate:组件将要更新

ComponentDidUpdate:组件已经更新

ComponentwillUnmount:组件将要卸载

  1. import React,{Component} from 'react';
  2. import ReactDOM from 'react-dom';
  3. class ComLife extends Component{
  4. constructor(props){
  5. super(props)//调用继承Component的构造函数
  6. this.state = {
  7. msg:"hello world"
  8. }
  9. console.log("constructor构造函数")
  10. }
  11. componentWillMount(){
  12. console.log("componentWillMount组件将要渲染")
  13. }
  14. componentDidMount(){
  15. console.log("componentDidMount组件渲染完毕")
  16. }
  17. componentWillReceiveProps(){
  18. console.log("componentWillReceiveProps组件将要接收新的state和props")
  19. }
  20. shouldComponentUpdate(){
  21. //如果希望更新,就返回为真,不希望更新就返回为false
  22. console.log("进行判断是否要更新内容")
  23. console.log(this.state.msg)
  24. if(this.state.msg==="hello world"){
  25. console.log(true)
  26. return true
  27. }else{
  28. console.log(false)
  29. return false
  30. }
  31. }
  32. componentWillUpdate(){
  33. console.log('componentWillUpdate组件将要更新')
  34. }
  35. componentDidUpdate(){
  36. console.log('componentDidUpdate组件更新完毕')
  37. }
  38. componentWillUnmount(){
  39. console.log('componentWillUnmount移除')
  40. }
  41. render(){
  42. console.log('render渲染函数')
  43. return (
  44. <div>
  45. <h1>{this.state.msg}</h1>
  46. <button onClick={this.changeMsg}>组件更新</button>
  47. </div>
  48. )
  49. }
  50. changeMsg=()=>{
  51. this.setState({
  52. msg:"hello 老陈"
  53. })
  54. }
  55. }
  56. class ParentCom extends Component{
  57. constructor(props){
  58. super(props)
  59. this.state = {
  60. isShow:true
  61. }
  62. }
  63. render(){
  64. if(this.state.isShow){
  65. return (
  66. <div>
  67. <button onClick={this.removeCom}>移除comlife</button>
  68. <ComLife></ComLife>
  69. </div>
  70. )
  71. }else{
  72. return <h1>comlife已移除</h1>
  73. }
  74. }
  75. removeCom=()=>{
  76. this.setState({
  77. isShow:false
  78. })
  79. }
  80. }
  81. ReactDOM.render(
  82. <ParentCom></ParentCom>,
  83. document.querySelector('#root')
  84. )

二、表单输入

注意:必须绑定value和onChange事件

  1. import React from 'react';
  2. class SearchCom extends React.Component{
  3. constructor(props){
  4. super(props)
  5. this.state = {
  6. value:"",
  7. result:null
  8. }
  9. }
  10. render(){
  11. return (
  12. <div>
  13. <input type="text" placeholder="请输入查询的省份" onKeyDown={this.searchEvent} value={this.state.value} onChange={this.changeEvent} />
  14. <div>
  15. <h2>查询结果:</h2>
  16. <div>
  17. {this.state.result}
  18. </div>
  19. </div>
  20. </div>
  21. )
  22. }
  23. searchEvent=(e)=>{
  24. if(e.keyCode===13){
  25. console.log(e.keyCode)//当keycode是回车的时候在进行执行查询
  26. console.log(e.target.value)
  27. console.log(this.props.provincesObj[e.target.value])
  28. if(this.props.provincesObj[e.target.value]===undefined){
  29. this.setState({
  30. result:<h2>输入错误,不是省份。请输入正确的省份</h2>
  31. })
  32. }else{
  33. this.setState({
  34. result:(
  35. <div>
  36. <div>确诊人数:{this.props.provincesObj[e.target.value].confirm}</div>
  37. <div>死亡人数:{this.props.provincesObj[e.target.value].dead}</div>
  38. <div>治愈人数:{this.props.provincesObj[e.target.value].heal}</div>
  39. </div>
  40. )
  41. })
  42. }
  43. }
  44. }
  45. changeEvent=(e)=>{
  46. this.setState({
  47. value:e.target.value
  48. })
  49. }
  50. }
  51. export default SearchCom