Reactjs 将类组件更改为功能组件

Reactjs 将类组件更改为功能组件,reactjs,Reactjs,我是一个新手,我正在尝试学习如何将一个类组件转换成一个功能组件。这个概念是有道理的,但我对如何处理多个状态有点困惑 class PostListWrapper extends React.Component { constructor(props) { super(props); this.state = { displayCategory: "View All", posts: props.posts, postCat

我是一个新手,我正在尝试学习如何将一个类组件转换成一个功能组件。这个概念是有道理的,但我对如何处理多个状态有点困惑

class PostListWrapper extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      displayCategory: "View All",
      posts: props.posts,
      postCategories: props.postCategories
    };
    this.setCategory = this.setCategory.bind(this);
  }
  setCategory(post_category) {
    this.setState({
      displayCategory: post_category
    });
  }

  render() {
    return <FilterSection setCategory={this.setCategory} state={this.state} posts={this.state.posts} />;
  }
}
类PostListWrapper扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
displayCategory:“查看全部”,
帖子:道具,帖子,
后分类:道具。后分类
};
this.setCategory=this.setCategory.bind(this);
}
设置类别(职位类别){
这是我的国家({
显示类别:post_类别
});
}
render(){
回来
}
}
到目前为止我有

const PostListWrapper = () => {

  const [displayCategory, setDisplayCategory] = React.useState("View All");
  const [posts, setPosts] = React.useState(posts);
  const [postCategories, setPostCategories] = React.useState(postCategories);

  const setCategory = () => setDisplayCategory(postCategories);

    return (
      <FilterSection setCategory={this.setCategory} state={this.state} posts={this.state.posts} />
    );
}
constpostlistwrapper=()=>{
const[displayCategory,setDisplayCategory]=React.useState(“查看全部”);
const[posts,setPosts]=React.useState(posts);
const[postCategories,setPostCategories]=React.useState(postCategories);
const setCategory=()=>setDisplayCategory(后分类);
返回(
);
}

我只是不知道如何处理
const setCategory=()=>setDisplayCategory(postCategories)

在您的类版本中,
post\u category
是子组件传递给
setCategory
的参数。在功能组件中,您已经有了更新类别状态的更新程序函数,即状态分派函数
setPostCategories
,因此将其向下传递:

<FilterSection setCategory={setPostCategories} 

post\u category
是类版本中的
FilterSection
中的一个参数,为什么不在函数版本中执行相同的操作呢?