第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

【備戰(zhàn)春招】第3天 React組件與通信

標(biāo)簽:
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绑定,代码过长可以直接提取出来,然后直接在代码处直接执行,

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消