reactjs / react-php-v8js

PHP library that renders React components on the server
Other
1.33k stars 127 forks source link

Uncaught ReferenceError: Component is not defined #27

Open laukaichung opened 8 years ago

laukaichung commented 8 years ago

How can I use Browserify or webpack to compile a es6 script that works with reach-php-v8js? I have tried the following but it looks like the js string produced by $rjs->getJS("app", "GLOB"); fails to access the component ItemPage in build.js which isn't in the global scope.

ReactDOM.render(React.createElement(ItemPage, {"value":"something"}), app);

item_page.js

class ItemPage extends React.Component {
    render(){
        return <div>{this.props.value}</div>
    }
}

After transpiling the es6 code into es5 with browserify, build.js by this command,

browserify item_page.js -o /js/build.js -t [ babelify --presets [ es2015 react ] ]

it ends up like this

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var ItemPage = function (_React$Component) {
    _inherits(ItemPage, _React$Component);

    function ItemPage() {
        _classCallCheck(this, ItemPage);

        return _possibleConstructorReturn(this, Object.getPrototypeOf(ItemPage).apply(this, arguments));
    }
    _createClass(ItemPage, [{
        key: "render",
        value: function render() {
            return React.createElement(
                "div",
                null,
                "Testing"
            );
        }
    }]);

    return ItemPage;
}(React.Component);

},{}]},{},[1]);

Error in Chrome:

Uncaught ReferenceError: ItemPage is not defined

Html output

<html>
<head>
    <title>React page</title>
    <!-- css and stuff -->
</head>
<body>
<!-- render server content here -->
<div id="app"><?php echo $rjs->getMarkup(); ?></div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.2.0/react.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.2.0/react-dom.min.js"></script>
<script src="/js/build/build.js"></script>
<script>
    ReactDOM.render(React.createElement(ItemPage, {"value":"something"}), app);</script>
   <!-- This is the result of echo $rjs->getJS($mainId, "GLOB"); -->
</script>
</body>
</html>
realtebo commented 7 years ago

no reply from author of this tool? have you solved?

chez14 commented 7 years ago

It seems like a human error.

Did you realize that you haven't export it?

class ItemPage extends React.Component {
    render(){
        return <div>{this.props.value}</div>
    }
}

should be this?

class ItemPage extends React.Component {
    render(){
        return <div>{this.props.value}</div>
    }
}
export default ItemPage

Anyway, it's a repository for php plugin that processes Javascript syntax (and it seems like it's getting rotten :stuck_out_tongue:). And anyway, try to ask on Stackoverflow first so you'll get answered more quickly, if you think it's a bug, feel free to create an issue.

AndriySand commented 6 years ago

If you are using Rails, then possible cause of error is that you added //= require react //= require react_ujs //= require components into your app/assets/javascripts/application.js

SillyHead commented 6 years ago

you are missing the Component. i have the same problem but what i did is i declare it on here.

import React, {Component} from 'react'; hope it helps