ReactJS - access to props child in render for props transfer -
when try access {this.props}
in render, crashes page. doing wrong here?
var react = require('react'); var defaultlayout = react.createfactory(require('../layouts/default')); var reactgridlayout = react.createfactory(require('react-grid-layout')); var desktopstore = require("../stores/desktopstore"); var _ = require('lodash'); // var classnames = require('classnames'); var homepage = react.createclass({ displayname: 'index.jsx', getinitialstate: function(){ return { zoomed: desktopstore.get('zoom'), layout: this.generatelayout() }; }, getdefaultprops: function() { return { layout: defaultlayout, grid: { items: 20, cols: 80, rowheight: 30, verticalcompact: false, autosize: false, isresizable: false, margin: [5,5] } }; }, generategriddom: function(){ return _.map(_.range(this.props.grid.items), function(i) { return (<div key={i}><span classname="text">{i}</span></div>); }); }, generatelayout: function(){ var p = this.props; var layout = _.map(new array(p.grid.items), function(item, i) { return {x: 0, y: 1, w: 1, h: 1, i: i}; }); return layout; }, render: function() { var parentclassstring = "desktop"; if(this.state.zoomed){ parentclassstring += " zoomed"; } return ( var grid = this.props.grid; // crashes page <div classname={parentclassstring}> <reactgridlayout classname="gridlayout" layout={this.state.layout} {...this.props.grid} > // crashes page {this.generategriddom()} </reactgridlayout> </div> ); } }); module.exports = homepage;
you can't define var grid = this.props.grid;
within return
, put before return
. render method supposed return single root dom node or react component.
if in webpack build output should point out var
unexpected token:
error in ./file.js module build failed: syntaxerror: unexpected token (56:12) 54 | 55 | return ( 56 | var grid = this.props.grid; // crashes page | ^
Comments
Post a Comment