• React中父子组件参数传递讲解


    结合案例:github搜索案例

    • 案例结果展示如下图
      在这里插入图片描述
    1.父容器代码
    import React, { Component } from 'react'
    import Search from './components/Search'
    import List from './components/List'
    export default class App extends Component {
    	state = { //初始化状态
    		users:[], //users初始值为数组
    		isFirst:true, //是否为第一次打开页面
    		isLoading:false,//标识是否处于加载中
    		err:'',//存储请求相关的错误信息
    	} 
    	//更新App的state
    	updateAppState = (stateObj)=>{
    		this.setState(stateObj)
    	}
    	render() {
    		return (
    			<div className="container">
    				<Search updateAppState={this.updateAppState}/>
    				<List {...this.state}/>
    			</div>
    		)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    说明:父容器中包含了两个组件模块,分别是Search和List,用来搜索和显示

    2.搜索Search子模块代码
    import React, { Component } from 'react'
    import axios from 'axios'
    
    export default class Search extends Component {
    
    	search = ()=>{
    		//获取用户的输入(连续解构赋值+重命名)
    		const {keyWordElement:{value:keyWord}} = this
    		//发送请求前通知App更新状态
    		this.props.updateAppState({isFirst:false,isLoading:true})
    		//发送网络请求
    		axios.get(`/api1/search/users?q=${keyWord}`).then(
    			response => {
    				//请求成功后通知App更新状态
    				this.props.updateAppState({isLoading:false,users:response.data.items})
    			},
    			error => {
    				//请求失败后通知App更新状态
    				this.props.updateAppState({isLoading:false,err:error.message})
    			}
    		)
    	}
    	render() {
    		return (
    			<section className="jumbotron">
    				<h3 className="jumbotron-heading">搜索github用户</h3>
    				<div>
    					<input ref={c => this.keyWordElement = c} type="text" placeholder="输入关键词点击搜索"/>&nbsp;
    					<button onClick={this.search}>搜索</button>
    				</div>
    			</section>
    		)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    3.展示Lisi子模块代码
    import React, { Component } from 'react'
    import './index.css'
    export default class List extends Component {
    	render() {
    		const {users,isFirst,isLoading,err} = this.props
    		return (
    			<div className="row">
    				{
    					isFirst ? <h2>欢迎使用,输入关键字,随后点击搜索</h2> :
    					isLoading ? <h2>Loading......</h2> :
    					err ? <h2 style={{color:'red'}}>{err}</h2> :
    					users.map((userObj)=>{
    						return (
    							<div key={userObj.id} className="card">
    								<a rel="noreferrer" href={userObj.html_url} target="_blank">
    									<img alt="head_portrait" src={userObj.avatar_url} style={{width:'100px'}}/>
    								</a>
    								<p className="card-text">{userObj.login}</p>
    							</div>
    						)
    					})
    				}
    			</div>
    		)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    父子参数传递分析

    1.子(Search)传父(App)

    首先在父容器APP的state中创建状态属性,再传给子模块Search一个函数updateAppState来更新App的state。
    然后Search子模块通过传过来的updateAppState函数讲自己的数据更新到全局的State中去。

    2.父(App)传子(List)

    直接通过将state中的属性传给子模块List即可。

  • 相关阅读:
    【华为OD机试真题 python】热点网站统计 【2022 Q4 | 200分】
    Service Worker cache 相比 HTTP cache 的一些优点
    指定程序在哪个GPU上运行
    【快速排序介绍】
    航天智信:严控航天系统研发安全,助力建设“航天强国”
    循环神经网络 - 循环神经网络的简洁实现
    搭建GraphQL服务
    STL标准模板库
    用友畅捷通文吉:如何通过智能运维提升稳定性保障
    群接龙大团长有哪些,群接龙大团长如何对接?
  • 原文地址:https://blog.csdn.net/shooter7/article/details/132701985