Reactjs 功能部件中的孔型减速器
我的react功能组件中的减速器无法通过, 请帮我把这个修好 这是我的减速机:Reactjs 功能部件中的孔型减速器,reactjs,redux,Reactjs,Redux,我的react功能组件中的减速器无法通过, 请帮我把这个修好 这是我的减速机: export const addPost = (value : String) => { return { type:actionTypes.ADD_POST, value, } } 我试图通过道具,但它给我的功能组件中的错误,有什么建议吗 Func组件: const Posts : React.FunctionComponent = () =&g
export const addPost = (value : String) => {
return {
type:actionTypes.ADD_POST,
value,
}
}
我试图通过道具,但它给我的功能组件中的错误,有什么建议吗
Func组件:
const Posts : React.FunctionComponent = () => {
const submitPost = () => {
let {addPost} = props.addPost
let titleValue = titleRef.current
let descriptionValue = descriptionRef.current
if (titleValue && descriptionValue) {
if ( titleValue.value === '' || descriptionValue.value === '' ) {
toast.info('Could not add new topic')
} else {
axios.post(`http://localhost:4000/addPost`, {
title : titleValue.value,
description : descriptionValue.value,
username : localStorage.getItem('username')
})
.then((res) => {
toast.info('Topic added succesfuly')
history.push('/homepage')
console.log('--------res', res);
})
.catch((err) => {
console.log('--------err', err);
})
}
}
}
在功能组件中,导入useDispatch 如果假设包含所有操作的操作文件名为myActions.js,则可以导入所有操作,如
import { useDispatch } from 'react-redux';
import * as actions from './myActions'
现在使用useDispatch,您可以在其中传递操作
在您的组件使用中,它是这样的
const dispatch = useDispatch();
dispatch(actions.addPost())
您是否尝试过使用
useDispatch
来自'react-redux
的它给我带来了错误
您可以添加整个错误吗?您的Posts函数中似乎缺少props
参数。我认为应该是让{addPost}=props代码>但是之后你再也不用它了,所以可能没关系。dispatch(actions.addPost())是的,它会将一个动作分派到redux,就像连接的类组件一样,你能告诉我如何在分派中传递值吗?我是这样做的({type:“ADD_POST”}),但我不知道如何在它中传递值,您可以直接传递它。假设您想要传递字符串“Nika”,那么您可以执行类似dispatch(actions.addPost(“Nika”))的操作