在React中仅传递一项道具

时间:2020-09-26 15:54:05

标签: reactjs react-props

我正在使用React构建一个博客应用程序。我有一个索引,提要和发布组件。我的Feed组件会动态生成帖子列表。我想要的是单击Feed中的一篇文章,并使页面仅呈现该文章。我无法弄清楚如何捕获/传递仅用于单击要渲染的帖子的道具信息。

为简洁起见,这里只是相关代码:

    this.state = {
  view: 'feed',
  blogs: []
}

  changeView(option) {
this.setState({
  view: option
});

}

renderView(){ const {view} = this.state;

if (view === 'feed') {
  return <Feed handleClick={() => this.changeView('anypostview') blog={this.state.blogs} 
  view={this.state.view}/>
} else {
  return <Post blogs={this.state.blogs} view={this.state.view}/>
}

}

我的click事件正在注册,但是我不知道如何仅捕获被单击的帖子的道具信息并将其发送到Post组件。

2 个答案:

答案 0 :(得分:0)

在单击特定帖子期间,将帖子传递到DECLARE @Countries TABLE ( [Country] VARCHAR(5) ) DECLARE @Country VARCHAR(5) -- For looping INSERT INTO @Countries ([Country]) SELECT DISTINCT SUBSTRING(Address, 1, 2) FROM Main_table WHILE (SELECT COUNT(1) FROM @Countries > 1) BEGIN SELECT TOP 1 @Country = [Country] FROM @Countries /* Your code goes here ******************************/ DELETE FROM @Countries WHERE [Country] = @Country END ,然后设置父组件的状态,在此组件中,父组件的状态为handleClick,将其更改为() => this.changeView('anypostview'),然后使用该父组件,在这种情况下,您将不需要定义或使用状态(individualPost?: Post) => this.changeView('anypostview', individualPost)

答案 1 :(得分:0)

> blog={this.state.blogs}

此代码将为您返回空白博客数组。©认为您可以使用setState为博客数组编写另一个函数

>   blogfunc = (blog) => {
>     this.setState({
>       blog: blog,
>     });   };

在博客中添加新内容时,调用此函数并显示在页面上。