如何在反应中将功能传输到功能组件
How to transport a function to a functional component in react
我是 react 和 typescript 的新手,在将函数从文件 App.tsx
传输到名为 WordAddingForm.tsx
的功能组件时遇到了一些问题
提前致谢!
所以这是我的 App.tsx
文件:
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);
我需要将函数 getWords
传输到组件
这是我的 WordAddingForm.tsx
文件:
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)
并且我需要将函数 getWords
传输到 <WordAddingForm/>
以在该组件中使用它,并在向数据库添加新单词后再次更新单词列表
喜欢 <WordAddingForm props={getWords}/>
或 <WordAddingForm {getWords}/>
没有帮助
<WordAddingForm getWords={getWords} />
这就是您将其传递给您的组件的方式。在WordAddingForm
内部使用时,必须通过参数接收props对象:
interface Props {
getWords: () => Promise<any>
}
const WordAddingForm: FC<Props> = (props) => {
// use your function as props.getWords()
}
我是 react 和 typescript 的新手,在将函数从文件 App.tsx
传输到名为 WordAddingForm.tsx
的功能组件时遇到了一些问题
提前致谢!
所以这是我的 App.tsx
文件:
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);
我需要将函数 getWords
传输到组件
这是我的 WordAddingForm.tsx
文件:
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)
并且我需要将函数 getWords
传输到 <WordAddingForm/>
以在该组件中使用它,并在向数据库添加新单词后再次更新单词列表
喜欢 <WordAddingForm props={getWords}/>
或 <WordAddingForm {getWords}/>
没有帮助
<WordAddingForm getWords={getWords} />
这就是您将其传递给您的组件的方式。在WordAddingForm
内部使用时,必须通过参数接收props对象:
interface Props {
getWords: () => Promise<any>
}
const WordAddingForm: FC<Props> = (props) => {
// use your function as props.getWords()
}