Search code examples
javascriptreactjsuser-interfaceuser-experiencereact-router-dom

how to change a component without rendering the other component in reactjs and react-router-dom


i have three component Header,Sidebar,and another one is the center . the center fetch data(all datas-post). In each post there will be a button to see present full post . now the full-post component will be replacing the all post-component. I dont want to render all other component. Just want to render the full-post component.Now How can I do this. My app.js containing the three main component

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 component

  render(){

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

now all the post are in postContent component . Now A component postdetail will be there which when a button will be click it will replace the postContent in this center component


Solution

  • You would need a route for each post that is rendered via url parameters. Notice :id for rendering a specific post. When that post is reached it can fetch its needed data as well.

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