網站首頁 編程語言 正文
一、Props 是什么
先來看一個 demo :
function Chidren(){ return <div> 我是子組件 </div> } /* props 接受處理 */ function Father(props) { const { children , mes , renderName , say ,Component } = props const renderFunction = children[0] const renderComponent = children[1] /* 對于子組件,不同的props是怎么被處理 */ return ( <div> { renderFunction() } { mes } { renderName() } { renderComponent } <Component /> <button onClick={ () => say() } > 觸發更改 </button> </div> ) } /* props 定義綁定 */ class App extends React.Component{ state={ mes: "hello,React" } node = null say= () => this.setState({ mes:'let us learn React!' }) render(){ return <div> <Father mes={this.state.mes} // ① props 作為一個渲染數據源 say={ this.say } // ② props 作為一個回調函數 callback Component={ Chidren } // ③ props 作為一個組件 renderName={ ()=><div> my name is YinJie </div> } // ④ props 作為渲染函數 > { ()=> <div>hello,world</div> } { /* ⑤render props */ } <Chidren /> { /* ⑥render component */ } </Father> </div> } }
我們看一下輸出結果:
當點擊觸發更改時就能夠調用回調更改數據源:
所以 props 可以是:
① props 作為一個子組件渲染數據源。
② props 作為一個通知父組件的回調函數。
③ props 作為一個單純的組件傳遞。
④ props 作為渲染函數。
⑤ render props , 和④的區別是放在了 children 屬性上。
⑥ render component 插槽組件。
二、props children模式
我們先來看看 prop + children 的幾個基本情況:
1. props 插槽組件
<Container> <Children> </Container>
上述可以在 Container 組件中,通過 props.children 屬性訪問到 Children 組件,為 React element 對象。
作用:
- 可以根據需要控制 Children 是否渲染。
- 像上一節所說的, Container 可以用 React.cloneElement 強化 props (混入新的 props ),或者修改 Children 的子元素。
舉一個用React.cloneElement 強化 props 的例子,多用于編寫組件時對子組件混入新的 props,下面我們要做一個導航組件,我們希望它的結構如下:
<Menu> <MenuItem > active </MenuItem> <MenuItem> disabled </MenuItem> <MenuItem > xyz </MenuItem> </Menu>
我們想給每個 MenuItem 子組件都添加 index 屬性,這個事情不應該讓用戶手動添加,最好是可以在 Menu 組件中自動為每個 MenuItem 子組件添加上,并且 Menu 組件還應該判斷子組件的類型,如果子組件的類型不是 MenuItem 組件就報錯。
Menu.tsx:
const Menu: React.FC<MenuProps> = (props) => { // ... 一些操作 const renderChildren = () => { // 讓子級的children都是 menuItem,有不是的就報錯 return React.Children.map(children, (child, index) => { const childElement = child as React.FunctionComponentElement<MenuItemProps> const { displayName } = childElement.type if(displayName === 'MenuItem' || displayName === "SubMenu") { return React.cloneElement(childElement, { index: index.toString() }) } else { console.error('warning: Menu has a child whitch is not a MenuItem') } }) } return ( <ul className={classes} style={style} data-testid="test-menu"> <MenuContext.Provider value={passedContext}> {renderChildren()} </MenuContext.Provider> </ul> ) }
在 Menu 組件中我們通過 React.children.map 來循環子組件,通過 child.type 可以獲取到每個子組件的 displayName 靜態屬性,這個在子組件中有定義:
通過子組件的 displayName 來判斷是否是我們需要的 MenuItem,如果是的話就調用 React.cloneElement 來為子組件添加 index 屬性。
2. render props模式
<Container> { (ContainerProps)=> <Children {...ContainerProps} /> } </Container>
這種情況,在 Container 中, props.children 屬性訪問到是函數,并不是 React element 對象,我們應該調用這個函數:
function Container(props) { const ContainerProps = { name: 'alien', mes:'let us learn react' } return props.children(ContainerProps) }
這種方式作用是:
1 根據需要控制 Children 渲染與否。
2 可以將需要傳給 Children 的 props 直接通過函數參數的方式傳遞給執行函數 children 。
3. render props模式
如果 Container 的 Children 既有函數也有組件,這種情況應該怎么處理呢?
<Container> <Children /> { (ContainerProps)=> <Children {...ContainerProps} name={'haha'} /> } </Container>
const Children = (props)=> (<div> <div>hello, my name is { props.name } </div> <div> { props.mes } </div> </div>) function Container(props) { const ContainerProps = { name: 'alien', mes:'let us learn react' } return props.children.map(item=>{ if(React.isValidElement(item)){ // 判斷是 react elment 混入 props return React.cloneElement(item,{ ...ContainerProps },item.props.children) }else if(typeof item === 'function'){ return item(ContainerProps) }else return null }) } const Index = ()=>{ return <Container> <Children /> { (ContainerProps)=> <Children {...ContainerProps} name={'haha'} /> } </Container> }
這種情況需要先遍歷 children ,判斷 children 元素類型:
- 針對 element 節點,通過 cloneElement 混入 props ;
- 針對函數,直接傳遞參數,執行函數。
三、進階實踐
實現一個簡單的<Form> <FormItem>嵌套組件
接下來到實踐環節了。需要編寫一個實踐 demo ,用于表單狀態管理的<Form>
和<FormItem>
組件
-
<Form>
用于管理表單狀態; -
<FormItem>
用于管理<Input>
輸入框組件。,
編寫的組件能夠實現的功能是:
①Form
組件可以被 ref 獲取實例。然后可以調用實例方法submitForm
獲取表單內容,用于提交表單,resetForm
方法用于重置表單。
②Form
組件自動過濾掉除了FormItem
之外的其他React元素
③FormItem
中 name 屬性作為表單提交時候的 key ,還有展示的 label 。
④FormItem
可以自動收集<Input/>
表單的值。
App.js:
import React, { useState, useRef } from "react"; import Form from './Form' import FormItem from './FormItem' import Input from './Input' function App () { const form = useRef(null) const submit =()=>{ /* 表單提交 */ form.current.submitForm((formValue)=>{ // 調用 form 中的submitForm方法 console.log(formValue) }) } const reset = ()=>{ /* 表單重置 */ form.current.resetForm() //調用 form 中的 resetForm 方法 } return <div className='box' > <Form ref={ form } > <FormItem name="name" label="我是" > <Input /> </FormItem> <FormItem name="mes" label="我想對大家說" > <Input /> </FormItem> <FormItem name="lees" label="ttt" > <Input /> </FormItem> </Form> <div className="btns" > <button className="searchbtn" onClick={ submit } >提交</button> <button className="concellbtn" onClick={ reset } >重置</button> </div> </div> } export default App
Form.js:
class Form extends React.Component{ state={ formData:{} } /* 用于提交表單數據 */ submitForm=(cb)=>{ cb({ ...this.state.formData }) } /* 獲取重置表單數據 */ resetForm=()=>{ const { formData } = this.state Object.keys(formData).forEach(item=>{ formData[item] = '' }) this.setState({ formData }) } /* 設置表單數據層 */ setValue=(name,value)=>{ this.setState({ formData:{ ...this.state.formData, [name]:value } }) } render(){ const { children } = this.props const renderChildren = [] React.Children.forEach(children,(child)=>{ if(child.type.displayName === 'formItem'){ const { name } = child.props /* 克隆`FormItem`節點,混入改變表單單元項的方法 */ const Children = React.cloneElement(child,{ key:name , /* 加入key 提升渲染效果 */ handleChange:this.setValue , /* 用于改變 value */ value:this.state.formData[name] || '' /* value 值 */ },child.props.children) renderChildren.push(Children) } }) return renderChildren } } /* 增加組件類型type */ Form.displayName = 'form'
設計思想:
- 首先考慮到
<Form>
在不使用forwardRef
前提下,最好是類組件,因為只有類組件才能獲取實例。 - 創建一個 state 下的 formData屬性,用于收集表單狀態。
- 要封裝重置表單,提交表單,改變表單單元項的方法。
- 要過濾掉除了
FormItem
元素之外的其他元素,那么怎么樣知道它是不是FormItem
,這里教大家一種方法,可以給函數組件或者類組件綁定靜態屬性來證明它的身份,然后在遍歷 props.children 的時候就可以在 React element 的 type 屬性(類或函數組件本身)上,驗證這個身份,在這個 demo 項目,給函數綁定的 displayName 屬性,證明組件身份。 - 要克隆
FormItem
節點,將改變表單單元項的方法 handleChange 和表單的值 value 混入 props 中。
FormItem.js:
function FormItem(props){ const { children , name , handleChange , value , label } = props const onChange = (value) => { /* 通知上一次value 已經改變 */ handleChange(name,value) } return <div className='form' > <span className="label" >{ label }:</span> { React.isValidElement(children) && children.type.displayName === 'input' ? React.cloneElement(children,{ onChange , value }) : null } </div> } FormItem.displayName = 'formItem'
設計思想:
-
FormItem
一定要綁定 displayName 屬性,用于讓<Form>
識別<FormItem />
- 聲明
onChange
方法,通過 props 提供給<Input>
,作為改變 value 的回調函數。 -
FormItem
過濾掉除了input
以外的其他元素。
Input.js:
/* Input 組件, 負責回傳value值 */ function Input({ onChange , value }){ return <input className="input" onChange={ (e)=>( onChange && onChange(e.target.value) ) } value={value} /> } /* 給Component 增加標簽 */ Input.displayName = 'input'
設計思想:
- 綁定 displayName 標識
input
。 -
input
DOM 元素,綁定 onChange 方法,用于傳遞 value 。
下面通過函數組件再重寫一下:
App.js,FormItem.js 和 Input.js 還是一樣的,Form.js使用了 hooks 鉤子來管理狀態,并且通過forwardRef, useImperativeHandle,讓 App 組件訪問到 Form 中的方法:
import React, { useState, forwardRef, useImperativeHandle } from "react" const Form = (props, ref) =>{ const { children } = props const [ formData, setFormData ] = useState({}) useImperativeHandle(ref, () => ({ submitForm: submitForm, resetForm: resetForm })) /* 用于提交表單數據 */ const submitForm=(cb)=>{ cb(formData) } /* 獲取重置表單數據 */ const resetForm=()=>{ const newData = formData Object.keys(newData).forEach(item=>{ newData[item] = '' }) setFormData(newData) } /* 設置表單數據層 */ const setValue=(name,value)=>{ setFormData({ ...formData, [name]:value }) } const renderChildren = () => { return React.Children.map(children,(child)=>{ if(child.type.displayName === 'formItem'){ const { name } = child.props /* 克隆`FormItem`節點,混入改變表單單元項的方法 */ const Children = React.cloneElement(child,{ key:name , /* 加入key 提升渲染效果 */ handleChange: setValue , /* 用于改變 value */ value: formData[name] || '' /* value 值 */ },child.props.children) return Children } }) } return ( renderChildren() ) } /* 增加組件類型type */ Form.displayName = 'form' export default forwardRef(Form)
啟動項目,查看效果:
點擊提交,我們在輸入框里輸入的內容就能顯示在控制臺上。
為了體現出咱們這個嵌套組件的高可復用性,我們可以在根組件中隨意添加子項:
原文鏈接:https://blog.csdn.net/qq_49900295/article/details/127211050
相關推薦
- 2022-01-11 require from ‘xxx‘是如何找到node_modules目錄下的依賴包?
- 2022-12-05 Android自定義view實現有header和footer作為layout使用的滾動控件_Andr
- 2022-06-28 python神經網絡tfrecords文件的寫入讀取及內容解析_python
- 2023-05-13 python中數字列表轉化為數字字符串的實例代碼_python
- 2023-01-05 Python?range函數之生成器函數的示例_python
- 2022-05-19 徹底解決No?module?named?‘torch_geometric‘報錯的辦法_python
- 2023-01-19 Retrofit?創建網絡請求接口實例過程_Android
- 2023-01-02 Kotlin?RadioGroup與ViewPager實現底層分頁按鈕方法_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支