目錄
- 組件拆分
- todolist代碼優化
- react編程特點
- PropTypes與DefaultProps
github地址:https://github.com/cindygogogo/studyReact/tree/master/todolist
組件拆分
接上,將todoList組件進行拆分,結構如下
todolist組件結構.png
父子組件之間通過標簽屬性的方式進行通信
1.父組件向子組件傳值
- 父組件通過屬性的形式向子組件傳遞數據,既可以傳遞數據又可以傳遞方法
<TodoItem
content={item}
index={index}
deleteItem={this.handleItemDelete.bind(this)} />
- 子組件通過this.props中接收傳遞過來的方法和數據
<div >{this.props.content}</div>
2.子組件調用父組件的方法,修改父組件的內容
子組件通過this.props.func()就可以調用父組件的方法,父組件傳遞的函數 this指向要做綁定,借助這個方法對父組件的數據進行修改
Todoitem.js
import React, { Component } from 'react'
class TodoItem extends React.Component{
constructor (props) {
super(props)
this.handleClick = this.handleClick.bind(this)
}
render () {
return <div onClick={this.handleClick}>{this.props.content}</div>
}
handleClick () {
this.props.deleteItem(this.props.index)
}
}
export default TodoItem
在Todolist.js中引入Todoitem組件,修改<ul></ul>
標簽包裹的內容
import TodoItem from './TodoItem'
<ul>
{
this.state.list.map((item, index) => {
return (
<div>
<TodoItem
content={item}
index={index}
deleteItem={this.handleItemDelete.bind(this)}
/>
</div>
)
})
}
</ul>
todolist代碼優化
1.優化todoitem組件,更符合ES6的寫法,例如const { content } = this.props
完整代碼:
import React, { Component } from 'react'
class TodoItem extends React.Component{
constructor (props) {
super(props)
this.handleClick = this.handleClick.bind(this)
}
render () {
const { content } = this.props
return (
<div onClick={this.handleClick}>
{content}
</div>
)
}
handleClick () {
const { deleteItem, index } = this.props
deleteItem(index)
}
}
export default TodoItem
2.優化todilist組件
減少頁面中的JS邏輯getTodoItem ()
在組件初始化的時候,修改this綁定
this.handleInputChange = this.handleInputChange.bind(this)
使用更推薦的方法修改修改數據
const value = e.target.value
this.setState( () => ({
inputValue: value
}) )
完整代碼:
import React, {Fragment, Component} from 'react';
import TodoItem from './TodoItem'
import './style.css'
class Todolist extends React.Component{
constructor (props) {
super(props)
this.state = {
inputValue: '',
list: []
}
// 組件初始化的時候修改this綁定
this.handleInputChange = this.handleInputChange.bind(this)
this.handleBtnClick = this.handleBtnClick.bind(this)
this.handleItemDelete = this.handleItemDelete.bind(this)
}
render() {
return (
<Fragment>
<div>
<label htmlFor="insertArea">輸入內容</label>
<input
id="insertArea"
className="input"
value={this.state.inputValue}
onChange={this.handleInputChange}/>
<button onClick={this.handleBtnClick}>提交</button>
</div>
<ul>
{ this.getTodoItem() }
</ul>
</Fragment>
);
}
getTodoItem () {
return this.state.list.map((item, index) => {
return (
<TodoItem
key={index}
content={item}
index={index}
deleteItem={this.handleItemDelete}
/>
)
})
}
handleInputChange(e) {
const value = e.target.value
this.setState( () => ({
inputValue: value
}) )
}
handleBtnClick () {
this.setState((prevState) => ({
list: [...prevState.list, prevState.inputValue],
inputValue: ''
}))
}
handleItemDelete (index) {
this.setState((prevState)=>{
const list = [...prevState.list]
list.splice(index, 1)
return { list }
})
}
}
export default Todolist;
react編程特點
- react是聲明式開發減少dom操作的代碼量,根據數據構建頁面
- 可以與其他框架并存
- 組件化
- 單向數據流,單向傳遞。子組件只能使用,不能修改
- 是一個視圖層框架,僅負責數據和頁面渲染,大型項目的組件通信交給數據層框架比如Redux、flux
- 函數式編程:易維護、面向測試
PropTypes與DefaultProps
PropTypes:對參數類型進行規定、做必填校驗
DefaultProps:設置默認值
TodoItem.propTypes = {
test: PropTypes.string.isRequired,
content: PropTypes.string,
deleteItem: PropTypes.func,
index: PropTypes.number,
}
TodoItem.defaultProps = {
test: 'hello world'
}
官方文檔:https://zh-hans.reactjs.org/docs/typechecking-with-proptypes.html
(完)