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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React Component Inheritance 使用父方法和子方法

React Component Inheritance 使用父方法和子方法

慕田峪9158850 2022-01-07 19:06:32
背景我創(chuàng)建了一個功能齊全的組件。組件有state和props,里面有很多方法。根據操作系統(tǒng)(ios / android),我的組件應該以不同的方式工作。所以我通過if如下語句解決了這個問題。if( platform.os == 'ios') { ... } else { ... }問題是隨著代碼量的增加,可讀性出現問題,我決定為IOS和Android分別制作一個組件。首先想到的是繼承,因為 ES6 和 Typescript 支持類。概念圖是這樣的。然而,React 不推薦繼承。所以我只是要把被 props 覆蓋的函數交給 SpeechIOS 組件的渲染函數中的 Speech 組件。代碼如下。語音.tsxtype Props = {  team: number,  onSpeechResults: (result: string) => void  ...}type States = {  active: boolean;  error: string;  result: string;  ...};export default class Speech extends Component<Props,States> {  state = { ... };  constructor(props: Props) {    super(props);    ...  }  // render  render() {    ...    return (      <ImageBackground source={require("../images/default-background.jpeg")} style={styles.full}>        ...      </ImageBackground>    );  }  sendCommand = (code: number, speed: number, callback?: () => void) => { ... }  getMatchedSpell = (spellWord: string): { code: number, speed: number } => { ... }  onSpeechResults(e: Voice.Results) { ... };  ...}SpeechIOS.tsximport Speech from './Speech';type Props = {}type States = {}export default class SpeechIOS extends Component<Props,States> {  constructor(props: Props) {    super(props);    ...  }  // render  render() {    ...    return ( <Speech team="1" onSpeechResults={this.onSpeechResults}></Speech> );  }  sayHello() {    console.log("Hello!!");  }  // I want that Speech Component call this onSpeechResults function  onSpeechResults(result: string) {    this.setState({...});    let temp = this.getMatchedSpell( ... ); // which is in Speech Component    this.sendCommand( 10, 100 ... ); // which is in Speech Component    this.sayHello(); // which is in SpeechIOS component only    ... other things..  };}問題。如您所見,onSpeechResultsSpeechIOS Component 中的 which 使用 Speech Component 和 SpeechIOS Component 中的一些功能。那么,如何解決這個問題呢?我應該使用繼承嗎?
查看完整描述

3 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

或者,將任何通用邏輯分解為一個實用函數,例如SpeechUtil.ts,一個包含此共享邏輯和每個實用函數的全新文件,并將它們導出。然后,每個組件分別導入它們。這樣可以確保如果您更新該邏輯,它會影響兩個組件


查看完整回答
反對 回復 2022-01-07
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

你可以使用 React.createRef 來達到這個目的。下面是一個示例代碼。


import Speech from './Speech';


type Props = {}

type States = {}

export default class SpeechIOS extends Component<Props, States> {

    constructor(props: Props) {

        super(props);

        this.speech = React.createRef();

    }


    // render

    render() {

        ...

        return (<Speech ref={this.speech} team="1" onSpeechResults={this.onSpeechResults}></Speech>);

    }


    sayHello() {

        console.log("Hello!!");

    }


    // I want that Speech Component call this onSpeechResults function

    onSpeechResults(result: string) {

        this.setState({ ...});

        let temp = this.speech.current.getMatchedSpell(... ); // which is in Speech Component

        this.sendCommand(10, 100 ... ); // which is in Speech Component

        this.sayHello(); // which is in SpeechIOS component only

        ...other things..

    };

}


查看完整回答
反對 回復 2022-01-07
?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

您應該定義一個頂級組件來定義共享的道具和方法,并使用它們來呈現您的 ios 或 android 組件。把道具和方法傳給孩子。這是在反應中優(yōu)于繼承的組合。例子:


class Speech extends React.Component {


  state ={shared: 'state'}


  sharedMethod = () => {

    this.setState({blah: 'blah})

  }


  render() {

    const Root = Platform.select({

      ios: SpeechIOS,

      android: SpeechAndroid

    })


    return <Root onClick={this.sharedMethod}  shared={this.state.shared}/>

  }


}


查看完整回答
反對 回復 2022-01-07
  • 3 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號