Home > Net >  How to transport a function to a functional component in react
How to transport a function to a functional component in react

Time:10-22

i'm kinda new in react and typescript and have a little problems with transportation a function from file App.tsx to functional component named WordAddingForm.tsx THANKS IN ADVANCE!

So this is my App.tsx file:

import React, {FC, useState} from 'react';
import WordAddingForm from './components/WordAddingForm';
import {observer} from "mobx-react-lite";
import WordService from "./services/WordService"

const App: FC = () => {

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

  return (
    <div>
      <div><WordAddingForm/></div>
    </div>
  )
}

export default observer(App);

I need to transport function getWords to component And this is my WordAddingForm.tsx file:

import React, {FC, useContext, useState} from 'react';
import {Context} from "../index";
import {observer} from "mobx-react-lite";
import WordService from '../services/WordService';

const WordAddingForm: FC = () => {

    const [word, setWord] = useState<string>('')
    const [wordT, setWordT] = useState<string>('')
    const [instance, setInstance] = useState<string>('')
    const [instanceT, setInstanceT] = useState<string>('')
    const {store} = useContext(Context)

    function clear():void {
        setWord('')
        setWordT('')
        setInstance('')
        setInstanceT('')
    }

    async function createWord(word:string, wordT:string, instance:string, instanceT:string) {
        try {
          const response = await store.createWord(word, wordT, instance, instanceT)
          console.log(response)
          clear()
        } catch (e) {
          console.log(e)
        }
      }

    return (
        <div>
            <input
            onChange={e => setWord(e.target.value)}
            value={word}
            type="text"
            placeholder="word"
            />
            <input
            onChange={e => setWordT(e.target.value)}
            value={wordT}
            type="text"
            placeholder="word translation"
            />
            <input
            onChange={e => setInstance(e.target.value)}
            value={instance}
            type="text"
            placeholder="instance of using this word"
            />
            <input
            onChange={e => setInstanceT(e.target.value)}
            value={instanceT}
            type="text"
            placeholder="instance translation"
            />
            <button
            onClick={() => {
                createWord(word, wordT, instance, instanceT)
                clear()
                }}>
                add word  
            </button>
        </div>
    )
}

export default observer(WordAddingForm)

And i need to transport function getWords to <WordAddingForm/> to use it in this component and to update words list again after adding a new word to database Like <WordAddingForm props={getWords}/> or <WordAddingForm {getWords}/> does not help

CodePudding user response:

<WordAddingForm getWords={getWords} />

This is how you pass it to your component. When using inside WordAddingForm, you must receive props object via the parameters:

interface Props {
  getWords: () => Promise<any>
}
const WordAddingForm: FC<Props> = (props) => {
// use your function as props.getWords()
}
  • Related