使用React服务器端渲染传递道具

时间:2015-08-13 10:38:57

标签: node.js express reactjs isomorphic-javascript

从Node(express)服务器端渲染时,我无法将初始道具传递给我的React组件

这是我的组件的简短版本:

/* mycomponent.jsx */
import React, {PropTypes, Component} from 'react/addons'
import GoogleMap from 'google-map-react'
import Place from './place.jsx'

export default class EventsMapPage extends Component {

  static defaultProps = {
    center: [59.838043, 30.337157]
  }

  constructor (props) {
    super(props)
  }

  render () {
    const locations = this.props.locations
      .map(place => {
        const {id, ...coords} = place

        return (
          <Place
            key={id}
            {...coords}
            text={id}
          />
        )
      })

    return (
       <div className='map-canvas'>
         <GoogleMap
          center={this.props.center}
         >
          {locations}
        </GoogleMap>
      </div>
    )
  }
}

这就是服务器端

/* app.js */
require('babel/register')({ stage: 0 })

var ejs = require('ejs')
var express = require('express')
var venues = require('./venues')
var React = require('react/addons')
var MyComponent = require('./src/js/components/mycomponent.jsx')

var Component = React.createFactory(MyComponent)

var app = express()

app.get('/locations/:id', function (req, res, next) {
  var loc = venues[req.params.id]

  return res.render('location', {
    react: React.renderToString(Component({locations: [loc]}))
  })
})


app.listen(process.env.port || 2000)

尝试在render()方法中使用它时,似乎locations prop没有被传递给组件

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以在&#34; GoogleMaps&#34;中添加您的位置。零件。

{locatios}