dongleibeng5602 2017-03-07 13:38
浏览 67

PHP V8Js渲染反应用typescript编写

I've been writing react components with typescript, following the instructions in official typescript docs:

https://www.typescriptlang.org/docs/handbook/react-&-webpack.html

I am using V8Js php extension to render React on server side, but it seems I lack the understanding of what is the correct way using V8Js.

this is my react application: (client-side rendered)

http://codepen.io/MasterScripter/pen/xqRZQO

I tried to render on side-server:

<?php

$v8 = new V8Js();

$react = [];

// stubs, react
$react[] = "var console = {warn: function(){}, error: print}";
$react[] = "var global = {}";
$react[] = file_get_contents('./dist/react.js');
$react[] = "var React = global.React";

$react[] = file_get_contents('./dist/bundle.js');
$data = [
  'value' => 1,
  'onClick' => 'function'
];
$react[] = sprintf(
  "React.renderComponentToString(Square({data: %s}), print)",
  json_encode($data));
$react = implode(";", $react);

try {
  $v8->executeString($react);
} catch(V8JsException $e) {
  echo "
    File: {$e->getJsFileName()} 

    Line Number: {$e->getJsLineNumber()} 

    Source Line: {$e->getJsSourceLine()} 

    Trace: {$e->getJsTrace()}
  ";
}

'react.js' includes raw react & react-dom code, 'bundle.js' includes webpack bundle, same as the one from the pen above.

I get this error:

File: V8Js::compileString() 

Line Number: 209 

Source Line: module.exports = ReactDOM; 

Trace: ReferenceError: ReactDOM is not defined
at Object.setPrototypeOf.__proto__ (V8Js::compileString():209:18)
at __webpack_require__ (V8Js::compileString():47:30)
at Object.<anonymous> (V8Js::compileString():300:16)
at __webpack_require__ (V8Js::compileString():47:30)
at module.exports (V8Js::compileString():93:18)
at V8Js::compileString():96:10

any suggestions / hints how I can do this properly?

  • 写回答

1条回答 默认 最新

  • doudundian9558 2017-03-09 17:51
    关注

    Looks like you need to add

    $react[] = "var React = global.React, ReactDOM = global.ReactDOM, ReactDOMServer = global.ReactDOMServer;";
    

    instead of

    $react[] = "var React = global.React";
    

    (I haven't tested it; just based on this. I don't have much experience with V8JS either. I recommend reading this for a nice introduction.)

    评论

报告相同问题?

悬赏问题

  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路