React父組件如何調(diào)用子組件的方法推薦
調(diào)用方法:1、類組件中的調(diào)用可以利用React.createRef()、ref的函數(shù)式聲明或props自定義onRef屬性來實現(xiàn);2、函數(shù)組件、Hook組件中的調(diào)用可以利用useImperativeHandle或forwardRef拋出子組件ref來實現(xiàn)。
本教程操作環(huán)境:Windows7系統(tǒng)、react18版、Dell G3電腦。
在React中,我們經(jīng)常在子組件中調(diào)用父組件的方法,一般用props回調(diào)即可。但是有時候也需要在父組件中調(diào)用子組件的方法,通過這種方法實現(xiàn)高內(nèi)聚。有多種方法,請按需服用。
類組件中
1、React.createRef()
- 優(yōu)點:通俗易懂,用ref指向。
- 缺點:使用了HOC的子組件不可用,無法指向真是子組件
比如一些常用的寫法,mobx的@observer包裹的子組件就不適用此方法。
import React, { Component } from 'react'; class Sub extends Component { callback() { console.log('執(zhí)行回調(diào)'); } render() { return <div>子組件</div>; } } class Super extends Component { constructor(props) { super(props); this.sub = React.createRef(); } handleOnClick() { this.sub.callback(); } render() { return ( <div> <Sub ref={this.sub}></Sub> </div> ); } }
2、ref的函數(shù)式聲明
- 優(yōu)點:ref寫法簡潔
- 缺點:使用了HOC的子組件不可用,無法指向真是子組件(同上)
使用方法和上述的一樣,就是定義ref的方式不同。
... <Sub ref={ref => this.sub = ref}></Sub> ...
3、使用props自定義onRef屬性
- 優(yōu)點:假如子組件是嵌套了HOC,也可以指向真實子組件。
- 缺點:需要自定義props屬性
import React, { Component } from 'react'; import { observer } from 'mobx-react' @observer class Sub extends Component { componentDidMount(){ // 將子組件指向父組件的變量 this.props.onRef && this.props.onRef(this); } callback(){ console.log("執(zhí)行我") } render(){ return (<div>子組件</div>); } } class Super extends Component { handleOnClick(){ // 可以調(diào)用子組件方法 this.Sub.callback(); } render(){ return ( <div> <div onClick={this.handleOnClick}>click</div> <Sub onRef={ node => this.Sub = node }></Sub> </div>) } }
函數(shù)組件、Hook組件
1、useImperativeHandle
- 優(yōu)點: 1、寫法簡單易懂 2、假如子組件嵌套了HOC,也可以指向真實子組件
- 缺點: 1、需要自定義props屬性 2、需要自定義暴露的方法
import React, { useImperativeHandle } from 'react'; import { observer } from 'mobx-react' const Parent = () => { let ChildRef = React.createRef(); function handleOnClick() { ChildRef.current.func(); } return ( <div> <button onClick={handleOnClick}>click</button> <Child onRef={ChildRef} /> </div> ); }; const Child = observer(props => { //用useImperativeHandle暴露一些外部ref能訪問的屬性 useImperativeHandle(props.onRef, () => { // 需要將暴露的接口返回出去 return { func: func, }; }); function func() { console.log('執(zhí)行我'); } return <div>子組件</div>; }); export default Parent;
2、forwardRef
使用forwardRef拋出子組件的ref
這個方法其實更適合自定義HOC。但問題是,withRouter、connect、Form.create等方法并不能拋出ref,假如Child本身就需要嵌套這些方法,那基本就不能混著用了。forwardRef本身也是用來拋出子元素,如input等原生元素的ref的,并不適合做組件ref拋出,因為組件的使用場景太復雜了。
import React, { useRef, useImperativeHandle } from 'react'; import ReactDOM from 'react-dom'; import { observer } from 'mobx-react' const FancyInput = React.forwardRef((props, ref) => { const inputRef = useRef(); useImperativeHandle(ref, () => ({ focus: () => { inputRef.current.focus(); } })); return <input ref={inputRef} type="text" /> }); const Sub = observer(FancyInput) const App = props => { const fancyInputRef = useRef(); return ( <div> <FancyInput ref={fancyInputRef} /> <button onClick={() => fancyInputRef.current.focus()} >父組件調(diào)用子組件的 focus</button> </div> ) } export default App;
總結(jié)
父組件調(diào)子組件函數(shù)有兩種情況
- 子組件無HOC嵌套:推薦使用ref直接調(diào)用
- 有HOC嵌套:推薦使用自定義props的方式
到此這篇關(guān)于React父組件怎么調(diào)用子組件的方法的文章就介紹到這了,更多相關(guān)React父組件調(diào)用子組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react?component?function組件使用詳解
這篇文章主要為大家介紹了react?component?function組件的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11react-router browserHistory刷新頁面404問題解決方法
本篇文章主要介紹了react-router browserHistory刷新頁面404問題解決方法,非常具有實用價值,需要的朋友可以參考下2017-12-12