【问题标题】:how to change a component without rendering the other component in reactjs and react-router-dom如何在不渲染 reactjs 和 react-router-dom 中的其他组件的情况下更改组件
【发布时间】:2018-06-05 12:50:06
【问题描述】:

我有三个组件 Header,Sidebar,另一个是 center 。中心获取数据(所有数据发布)。在每个帖子中都会有一个按钮来查看当前的完整帖子。现在完整的帖子组件将替换所有的帖子组件。我不想渲染所有其他组件。只想渲染完整的帖子组件。现在我该怎么做。 我的 app.js 包含三个主要组件

export default class App extends React.Component{
  constructor(props){
    super(props);
  }
  render(){
    return (
      <ApolloProvider client = {client} >
        <div >
          <div className="row">
            <SideBar store={store} />
            <Center store={store} />
            <RightSlidePanel store={store}/>
          </div>
        </div>
      </ApolloProvider>
    )
  }
}

center.js 组件

  render(){

    return (
      <Provider store = {this.store}>
        <div className="center-bar nine columns ">
          <div className="row">
              <CenterHeader />
              <PostContent />
              <RightSide />
          </div>
        </div>
      </Provider>
    )

现在所有帖子都在 postContent 组件中。现在将出现一个组件postdetail,当单击按钮时,它将替换此center 组件中的postContent

【问题讨论】:

  • 请发布您的代码。
  • @Colin 请检查我更新了问题

标签: javascript reactjs user-interface user-experience react-router-dom


【解决方案1】:

您需要为每个通过 url 参数呈现的帖子设置一个路由。 注意 :id 用于呈现特定帖子。当到达该帖子时,它也可以获取所需的数据。

  render () {
    return (
      <Switch>
        <Route
          exact
          path={'/posts'}
          render={() => {
            return (
              <BlogSummaryList
                posts={this.state.blogPosts}
              />
            )
          }}
        />
        <Route
          exact
          path={'/posts/:id'}
          component={BlogPost}
        />
    )
  }

【讨论】:

  • 这是什么withBlogPostData(blogPost)?我认为它可能是一个组件?
  • 实际上使用简单的路由只会显示博客文章组件,侧边栏和标题以及所有
  • 它是一个高阶组件。标题和侧边栏不应该是路由的一部分,它们应该是外部上下文的一部分。
  • 那么这条路线会在哪里?在上面的问题代码中>
  • 在我的代码中我有这样的路由:索引路由到 Home (/posts),然后在 home 组件中我有上面的两条路由
猜你喜欢
  • 2019-04-28
  • 2017-11-14
  • 1970-01-01
  • 2019-01-15
  • 2016-09-05
  • 2018-01-24
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多