为了账号安全,请及时绑定邮箱和手机立即绑定

【备战春招】第3天 React组件与通信

标签:
React

**课程名称:** React16.4 快速上手

课程讲师: Dell

课程内容:
1、拆分组件TodoItem

import React from 'react'
class TodoItem extends React.Component {
	render(){
		return (
			<div> {this.props.content}</div>
		)
	}
}
export default TodoItem

2、在主组件TodoList中引用子组件TodoItem ,父组件通过属性的形式向子组件传递参数,子组件通过props接受父组件传递过来的参数

import React from 'react'
import TodoItem from './TodoItem'
class TodoList extends React.Component {
	constructor(props){
		super(props);
		this.state = {
			list:[
				'learn react',
				'learn english'
			],
			inputValue:''
		}
	}
	handleBthClick(){
		this.setState({
			list:[...this.state.list,this.state.inputValue],
			inputValue:''
		})
		this.state.list.push('hello world')
	}
	handleInputChange(e){]
		this.setState({
			inputValue:e.target.value
		})
	}
	handleItemClick(index){
		const list = [...this.state.list]
		list.splice(index,1)
		this.setState({
			list:list
		})
		
	}
	render(){
		return (
			<div>
				<div>
					<input onClick={this.handleInputChange.bind(this)} />
					<button onClick={this.handleBthClick.bind(this)}>add </button>
				</div>
				<ul>
					{
·						this.state.list.map((item,index) => {
							return <TodoItem content={item} key={index} index={index}/>
						})
					}
					
				</ul>
			</div>
		)
	}
}

3、子组件向父组件传值,子组件如果想和父组件通信,子组件要调用父组件传递过来的方法
子组件

import React from 'react'
class TodoItem extends React.Component {
	handleDelete(){
		this.props.Delete(this.props.index)
	}
	render(){
		return (
			<div onClick={this.handleDelete.bind(this)}> {this.props.content}</div>
		)
	}
}
export default TodoItem

父组件

import React from 'react'
import TodoItem from './TodoItem'
class TodoList extends React.Component {
	constructor(props){
		super(props);
		this.state = {
			list:[
				'learn react',
				'learn english'
			],
			inputValue:''
		}
		this.handleInputChange = this.handleInputChange.bind(this)
		this.handleBthClick = this.handleBthClick.bind(this)
		this.handleDelete = this.handleDelete.bind(this)
	}
	handleBthClick(){
		this.setState({
			list:[...this.state.list,this.state.inputValue],
			inputValue:''
		})
		this.state.list.push('hello world')
	}
	handleInputChange(e){]
		this.setState({
			inputValue:e.target.value
		})
	}
	
	handleDelete(index){
		const list = [...this.state.list]
		list.splice(index,1)
		this.setState({
			list:list
		})
	}
	render(){
		return (
			<div>
				<div>
					<input onClick={this.handleInputChange} />
					<button onClick={this.handleBthClick}>add </button>
				</div>
				<ul>
					{
·						this.state.list.map((item,index) => {
							return <TodoItem Delete={this.handleDelete} content={item} key={index} index={index}/>
						})
					}
					
				</ul>
			</div>
		)
	}
}

4、代码优化
子组件

import React from 'react'
class TodoItem extends React.Component {
constructor(props){
super(props)
	this.handleDelete = this.handleDelete.bind(this)
}
	handleDelete(){
		this.props.Delete(this.props.index)
	}
	render(){
	const {content} = this.props
		return (
			<div onClick={this.handleDelete}> {content}</div>
		)
	}
}
export default TodoItem

父组件

import React from 'react'
import TodoItem from './TodoItem'
class TodoList extends React.Component {
	constructor(props){
		super(props);
		this.state = {
			list:[
				'learn react',
				'learn english'
			],
			inputValue:''
		}
	}
	handleBthClick(){
		this.setState({
			list:[...this.state.list,this.state.inputValue],
			inputValue:''
		})
		this.state.list.push('hello world')
	}
	handleInputChange(e){]
		this.setState({
			inputValue:e.target.value
		})
	}
	
	handleDelete(index){
		const list = [...this.state.list]
		list.splice(index,1)
		this.setState({
			list:list
		})
	}
	getTodoItems(){
		return(
			this.state.list.map((item,index) => {
							return <TodoItem Delete={this.handleDelete.bind(this)} content={item} key={index} index={index}/>
						})
		)
	}
	render(){
		return (
			<div>
				<div>
					<input onClick={this.handleInputChange.bind(this)} />
					<button onClick={this.handleBthClick.bind(this)}>add </button>
				</div>
				<ul>
					{
·						this.getTodoItems()
					}
					
				</ul>
			</div>
		)
	}
}

课程收获:
这节课学习到了组件间的通信,首先父组件通过属性给子组件传值,子组件向父组件传值,需要调用父组件的方法才可以进行传值,优化this指向,在constructor中直接添加函数的bind绑定,代码过长可以直接提取出来,然后直接在代码处直接执行,

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
2
获赞与收藏
23

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消