Friday, 17 March 2017

How to pass props for component while hitting internal url directly?

I am doing Server Side Rendering and when I hit root url it work fine (localhost:8000/) ,and even clicking any link on homepage like (localhost:8000/black) also rendering component fine, but when I directly hit "localhost:8000/black" directly, I am blank about how to pass appropriate props for black on server so that I can get full html page from server. Below is code of render function on server.js.

  render(path, data={}) {
    match({ routes, location: path }, (error, redirectLocation, renderProps) => {
      if (error) {
        this.res.status(500).send(error.message)
      } else if (redirectLocation) {
        this.res.redirect(302, redirectLocation.pathname + redirectLocation.search)
      } else if (renderProps) {
        const appElement = (
            <RouterContext
                {...renderProps}
                createElement={(Component, props) =>
                  <Component data={data} {...props} />
                }
            />
        );
        this.res.status(200).send(renderToString(appElement))
      } else {
        this.res.status(404).send('Not found')
      }
    })
  }


via Prashant

No comments:

Post a Comment