Reactjs 当从功能组件传递到其他组件时,history.push()不起作用
我非常习惯于对组件进行分类。在那里,我可以将绑定到此的函数毫无问题地传递给其他组件。我认为功能组件也是如此 但是,以下代码根本不起作用:Reactjs 当从功能组件传递到其他组件时,history.push()不起作用,reactjs,Reactjs,我非常习惯于对组件进行分类。在那里,我可以将绑定到此的函数毫无问题地传递给其他组件。我认为功能组件也是如此 但是,以下代码根本不起作用: const Dropdown: React.FC<{onNewPost: any}> = (props) => { return( <div onClick={props.onNewPost}></div> ) } function AddMessage(props: IProps)
const Dropdown: React.FC<{onNewPost: any}> = (props) => {
return(
<div onClick={props.onNewPost}></div>
)
}
function AddMessage(props: IProps) {
const { conversationUUID } = props.match.params;
const navigateToNewPost = (postUUID: string) => {
props.history.push(`/app/messages/new/${postUUID}/`)
}
const onNewPost = () => {
// props.history.push('/example/') -> this works without problems
props.createPost(
conversationUUID,
navigateToNewPost
)
}
return(
<Dropdown onNewPost={onNewPost}/>}
)
}
const mapStateToProps = (state: AppState) => ({
})
const mapDispatchToProps = (dispatch: Dispatch) => bindActionCreators({
createPost
}, dispatch)
export default connect(mapStateToProps, mapDispatchToProps)(withRouter(AddMessage));
createPost
函数是什么样子的?我将其编辑为原始帖子。哦,我看到了错误。。。我忘记了axios呼叫前的等待。。。所以它只是跳转到回调,没有它需要的值。。。我的错。
export const createPost = (conversationUUID: string, callback: any) => async (dispatch: Dispatch) => {
try {
const res: any = authAxios.post('/posts/', {
conversation: conversationUUID,
})
if (callback) {callback(res.data.uuid)}
} catch(e) {
}
}