Reactjs useState钩子的setState函数的类型?

Reactjs useState钩子的setState函数的类型?,reactjs,typescript,react-hooks,typescript-typings,use-state,Reactjs,Typescript,React Hooks,Typescript Typings,Use State,我正在将React项目转换为Typescript 我有这样一种状态: AdminBlogPostContainer.tsx const [blogPost,setBlogPost] = useState<null | BLOGPOST>(null); return( <AdminBlogPostPage blogPost={blogPost as BLOGPOST} setBlogPost={setBlogPost} /> ); interfa

我正在将React项目转换为Typescript

我有这样一种状态:

AdminBlogPostContainer.tsx

const [blogPost,setBlogPost] = useState<null | BLOGPOST>(null);

return(
  <AdminBlogPostPage
    blogPost={blogPost as BLOGPOST}
    setBlogPost={setBlogPost}
  />
);
interface AdminBlogPostPage {
  blogPost: BLOGPOST,
  setBlogPost:            // <---- WHAT SHOULD I USE AS TYPE HERE ?
}

const AdminBlogPostPage: React.FC<AdminBlogPostPage> = (props) => {
  console.log("Rendering AdminBlogPostPage...");

  return(
    // ...
  );
};

export default AdminBlogPostPage;
const[blogPost,setBlogPost]=useState(null);
返回(

让我们从中的一些相关类型定义开始

这是
Dispatch
,但让我们将其分解,看看每个部分的含义

setBlogPost: (value: null | BLOGPOST | ((prevState: null | BLOGPOST) => null | BLOGPOST)) => void;
从外到内一次消化一块,我们得到以下解释:

  • setBlogPost:(值:…)=>void
    setBlogPost
    是一个接受参数
    并返回
    void
    的函数

    • value:null | BLOGPOST |((prevState:…)=>null | BLOGPOST)
      value
      要么是
      null
      ,要么是
      BLOGPOST
      ,要么是接受参数
      prevState
      并返回
      null
      的函数,要么是
      BLOGPOST

      • prevState:null | BLOGPOST
        prevState
        null
        BLOGPOST

界面管理{
blogPost:blogPost;

setBlogPost:Function;//无论如何,您不应该从子项更改父项状态谢谢您的回复。但是,无论我是否应该从子项的父项更改状态,有人知道在这种情况下,我应该使用什么类型的
setState
函数吗?无法作为副本关闭,因为答案不被接受,但是否则应该是…@PatrickRoberts谢谢!!这就是我要找的。所以在我的情况下,我应该使用:
React.Dispatch
?请随意回答这里,如果答案有效,我会接受你的答案。感谢接受的答案,继续从其他帖子中删除我的答案,并将其改为标准副本。
const [blogPost, setBlogPost] = useState<null | BLOGPOST>(null);
setBlogPost: (value: null | BLOGPOST | ((prevState: null | BLOGPOST) => null | BLOGPOST)) => void;
interface AdminBlogPostPage {
  blogPost: BLOGPOST;
  setBlogPost: Function;            // <---- You may use the Function type here
}