TypeScript-类型“ x”不可分配给类型“ IntrinsicAttributes&IntrinsicClassAttributes <Class>”

时间:2020-03-09 18:27:15

标签: reactjs typescript

我已经通过其他代码(使用标签[Component nameExample = {this.method} /])将类方法传递给了子组件,并且效果很好,但是当我尝试在另一个代码中做同样的事情时,我出现以下错误:

enter image description here

我该怎么做才能解决此错误?即使方法在类中,我也需要使用Interface或Type?

父组件(将通过方法的父组件):

import React from 'react';
import { IonApp, IonRouterOutlet, IonTabButton, IonLabel, IonTabBar, IonTabs, IonModal, IonButton, IonContent, IonHeader, IonToolbar, IonTitle } from '@ionic/react';
import { IonReactRouter } from '@ionic/react-router';
import { Modal, Nav, Navbar } from 'react-bootstrap';
import Chats from '../chats';
import Contato from '../contato';

interface mainProps {
    contatoProps: () => void;
}

class Main extends React.Component<any, {showContato: boolean, showRecebimento: boolean, showLara: boolean, showPagamento: boolean, showDashboard: boolean}>{ 
    constructor(props: any ){
        super(props);

        this.state = {
            showContato: false,
            showRecebimento: false,
            showLara: false,
            showPagamento: false,
            showDashboard: false,
        };

        this.showLaraTab = this.showLaraTab.bind(this);
        this.hideLaraTab = this.hideLaraTab.bind(this);
    }

    showLaraTab(value: boolean, tab: string){
        switch (tab) {
            case 'contato':
                this.setState({ showContato: value });
                break;
            case 'recebimento':
                this.setState({ showRecebimento: value });
                break;
            case 'lara':
                this.setState({ showLara: value });
                break;
            case 'pagamento':
                this.setState({ showPagamento: value });
                break;
            case 'dashboard':
                this.setState({ showDashboard: value });
                break;
        }
    }

    {/* Method that I want to pass in props  */}

    hideLaraTab(value: boolean, tab: string){
        switch (tab) {
            case 'contato':
                this.setState({ showContato: value });
                break;
            case 'recebimento':
                this.setState({ showRecebimento: value });
                break;
            case 'lara':
                this.setState({ showLara: value });
                break;
            case 'pagamento':
                this.setState({ showPagamento: value });
                break;
            case 'dashboard':
                this.setState({ showDashboard: value });
                break;
        }
    }

    render(){
        return (
            <IonApp>
                <IonReactRouter>
                    {/* Component with a hidden modal */}
                    <Contato contatoProps={this.hideLaraTab(false, 'contato')} /> 

                    <Chats />
                    <Navbar className="t-bd" fixed="bottom">
                        {/* Nav Content */}
                    </Navbar>

                </IonReactRouter>
            </IonApp>
        );
    }

}


export default Main;

子组件(将接收该方法的子组件):

import React from 'react';
import { IonModal, IonHeader, IonTitle, IonToolbar,  IonContent } from "@ionic/react";
import { SpeechRecognition } from '@ionic-native/speech-recognition/ngx';
import Main from '../main/index';
import toastr from 'toastr';
import '../../toastr.min.css';


class Contato extends React.Component<{}, { matches: string, isRecording: boolean }, Main>{
  constructor(props: any, private speechRecognition: SpeechRecognition ){
    super(props);

    this.state = {
      matches: '',
      isRecording: false,
    };


    this.checkAvaliable = this.checkAvaliable.bind(this);
    this.checkPermissions = this.checkPermissions.bind(this);
    this.getPermissions = this.getPermissions.bind(this);
    this.startListening = this.startListening.bind(this);
    this.stopListening = this.stopListening.bind(this);

  }

  checkAvaliable = () => {
    // Check feature available
    this.speechRecognition.isRecognitionAvailable()
    .then((available: boolean) => console.log(available))
  }

  checkPermissions = () => {
    // Check permission
    this.speechRecognition.hasPermission()
    .then((hasPermission: boolean) => console.log(hasPermission))
  }

  getLanguageList = () => {
    // Get the list of supported languages
    this.speechRecognition.getSupportedLanguages()
    .then(
      (languages: string[]) => console.log(languages),
      (error) => console.log(error)
    )
  }

  getPermissions = () => {
    // Request permissions
    this.speechRecognition.requestPermission()
    .then(
      () => console.log('Granted'),
      () => console.log('Denied')
    )
  }

  startListening = () => {
    let options = {
      language: 'pt-BR',
    };

    // Start the recognition process
    this.speechRecognition.startListening(options)
    .subscribe(
      (matches: string[]) => console.log(matches),
      (onerror) => console.log('error:', onerror)
    )
  }

  stopListening = () => {
    this.speechRecognition.stopListening().then(() => {
        this.setState({isRecording: false})
    });
  }

  render(){
    return(
    <>
      <IonModal isOpen={false}>
        <IonHeader>
          <IonToolbar>
            <div className="d-flex">
                <button className="btn" onClick={this.props.contatoProps}><i className="fas fa-arrow-left" /></button>
                <IonTitle>Novo Contato</IonTitle>
              </div>
          </IonToolbar>
        </IonHeader>

        <IonContent>
          <div className="mb-3">
            <h3>O que eu entendi...</h3>
            <span>{this.state.matches}</span>
          </div>
          <div className="row mt-5">
            <div className="col-12">
              <button className="btn btn-tiber w-100" onClick={this.checkAvaliable}>Check Avaliable</button>
            </div>
            <div className="col-12">
              <button className="btn btn-tiber w-100" onClick={this.checkPermissions}>Check Permissions</button>
            </div>
            <div className="col-12">
              <button className="btn btn-tiber w-100" onClick={this.getLanguageList}>Get Languages</button>
            </div>
            <div className="col-12">
              <button className="btn btn-tiber w-100" onClick={this.getPermissions}>Get Permission</button>
            </div>
            <div className="col-12">
              <button className="btn btn-tiber w-100" onClick={this.startListening}>Start Listening</button>
            </div>
          </div>
        </IonContent>

      </IonModal>

    </>
    );
  }


}



export default Contato;    

1 个答案:

答案 0 :(得分:1)

我认为您的问题是您正在传递hideLaraTab函数的结果作为道具:

<Contato contatoProps={this.hideLaraTab(false, 'contato')} /> 

相反,您想传递一个可以在Contato组件内部调用的函数:

<Contato contatoProps={() => this.hideLaraTab(false, 'contato')} /> 

我希望这会有所帮助。