react 中组件传值———消息订阅-发布机制(工具库pubsub-js)

GitHub地址

https://github.com/mroderick/PubSubJS

案例——两个兄弟组件通讯 ,search搜索框组件,list列表组件

1.Search.jsx

import React, { Component } from ‘react‘
import PubSub from ‘pubsub-js‘
import axios from ‘axios‘

export default class Search extends Component {

	search = ()=>{
		//获取用户的输入(连续解构赋值+重命名)
		const {keyWordElement:{value:keyWord}} = this
		//发送请求前通知List更新状态
		PubSub.publish(‘atguigu‘,{isFirst:false,isLoading:true})
		//发送网络请求
		axios.get(`/api1/search/users?q=${keyWord}`).then(
			response => {
				//请求成功后通知List更新状态
				PubSub.publish(‘atguigu‘,{isLoading:false,users:response.data.items})
			},
			error => {
				//请求失败后通知App更新状态
				PubSub.publish(‘atguigu‘,{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>
		)
	}
}

2.List.jsx

import React, { Component } from ‘react‘
import PubSub from ‘  -js‘
import ‘./index.css‘

export default class List extends Component {

	state = { //初始化状态
		users:[], //users初始值为数组
		isFirst:true, //是否为第一次打开页面
		isLoading:false,//标识是否处于加载中
		err:‘‘,//存储请求相关的错误信息
	} 

	componentDidMount(){
		this.token = PubSub.subscribe(‘atguigu‘,(_,stateObj)=>{
			this.setState(stateObj)
		})
	}

	componentWillUnmount(){
		PubSub.unsubscribe(this.token)
	}

	render() {
		const {users,isFirst,isLoading,err} = this.state
		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>
		)
	}
}


react 中组件传值———消息订阅-发布机制(工具库pubsub-js)

上一篇:JS 动态加载脚本的4种方法【转】


下一篇:一加7 nethunter + andrax + parrot 开机指导