我一直在使用typescript编写react组件,遵循官方typescript文档中的说明:
https://www.typescriptlang.org/docs/handbook/react-&-webpack.html
我正在使用V8Js php扩展在服务器端渲染React,但我似乎对使用V8Js的正确方式缺乏理解。
这是我的react应用程序:(客户端渲染)
http://codepen.io/MasterScripter/pen/xqRZQO
我尝试在端服务器上渲染:
<?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()} \n
Line Number: {$e->getJsLineNumber()} \n
Source Line: {$e->getJsSourceLine()} \n
Trace: {$e->getJsTrace()}
";
}'react.js‘包含原始的react & react-dom代码,'bundle.js’包含webpack包,与上面的笔中的包相同。
我得到了这个错误:
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有什么建议/提示可以让我正确地做到这一点吗?
https://stackoverflow.com/questions/42649793
复制相似问题