简体   繁体   中英

How to transport a function and an object to a functional component in react

I'm still have problems with typescript and react so please be condescending. Thank you in advance guys.

I'm trying to transport a function and an object to a component

So here is my component named WordItem

import React, {FC, useContext} from "react"
import { IWord } from "../../../models/IWord"
import {Context} from "../../../index";

const WordItem: FC<IWord> = (word) => {

    const {store} = useContext(Context)

    async function deleteWord () {
        await store.deleteWord(word.id)
    }

    return (
        <div key={word.id}>
            <div>
                <div>{word.word}</div>
                <div>{word.wordT}</div>
            </div>
            <div>
                <div>{word.instance}</div>
                <div>{word.instanceT}</div>
                <button onClick={() => {deleteWord()}}>delete</button>
            </div>
        </div>
    )
}

export default WordItem

And this is my App.tsx file:

import React, {FC, useContext, useState} from 'react';
import {Context} from "./index";
import {observer} from "mobx-react-lite";
import {IUser} from "./models/IUser";
import WordService from "./services/WordService"
import { IWord } from './models/IWord';
import WordItem from './components/UI/word/WordItem';

const App: FC = () => {

  const {store} = useContext(Context)
  const [users, setUsers] = useState<IUser[]>([])
  const [words, setWords] = useState<IWord[]>([])
  
  async function getWords() {
    try {
      const response = await WordService.fetchWords()
      setWords(response.data)
    } catch (e) {
      console.log(e)
    }
  }

    return (
      <div>

        <button onClick={getWords}>Get words</button>
        {words.reverse().map(word => <WordItem {...word}/>)}
        {words.length == 0? 'there is no words': ''}

      </div>
    )

  
}

export default observer(App);

As you can see i successfully transported object word in a component but i really dont know the way to transport a function also. <WordItem {getWords, ...word}/> or <WordItem getWords={getWords} { ...word}/> does not help. Like how to properly put then into <WordItem/>

If you want to pass down both the word variable and the getWords function to the WordItem component, you should do it as follow:

<WordItem word={word} getWords={getWords} />

Then you need to adapt your component prop types to match these props:

const WordItem: FC<{word: IWord, getWords: () => Promise<void>}> = ({word, getWords}) => {
...

I am using object destructuring in the code above to get the two props from the prop parameter which is equivalent to doing something like:

const WordItem: FC<{word: IWord, getWords: () => Promise<void>}> = (props) => {
  const word = props.word;
  const getWords = props.getWords;
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM