jsx传递给react-dom / server renderToString

时间:2018-05-01 18:19:28

标签: reactjs react-dom-server

我试图了解react-dom / server。

我有一个反应应用程序,使用express作为服务器。

我有一条明确的路线:

Private Sub btnCancelLines_Click(sender As Object, e As EventArgs) Handles btnCancelLines.Click
    MdiParent = Toolbox


    Try
        Dim orderNum As String = CStr(txtOrder.Text)
        Dim skuNum As String = CStr(txtSKU.Text)
        SQL.AddParam("@order", txtOrder.Text)
        SQL.AddParam("@SKU", txtSKU.Text)

        SQL.ExecQuery("update sop10200
                       set qtycance = qtytoinv, qtytoinv = '0', ATYALLOC = '0', QTYREMAI = '0'
                       where ITEMNMBR = @sku
                       and SOPNUMBE = @order;")
        If SQL.HasException(True) Then Exit Sub
        dgvData.DataSource = SQL.DBDT


        LoadGrid()

    Catch ex As System.InvalidCastException
        MessageBox.Show("Please use numbers",
                        "Error")
        Console.WriteLine("An error occured")
    Catch ex As Exception
        MessageBox.Show("An unknown error occured",
                        "Error")
    End Try
End Sub

如果我使用var server = app.listen(3000); app.get('/test', (req, res) => { const context = {} const html = ReactDOMServer.renderToString( <h1>foo</h1> ) if (context.url) { res.writeHead(302, { Location: context.url }) res.end() } else { res.write(html) res.end() } }); 运行服务器文件,则会收到此错误:

node app-server

我根据示例将JSX传递给renderToString: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/StaticRouter.md

如何让快速服务器文件处理这个JSX代码?

对于客户端,我使用webpack和babel loader,它工作正常。

1 个答案:

答案 0 :(得分:0)

感谢@azium。

解决方案是首先使用babel-cli转换ES6 / JSX文件。

我使用了babel-cli command

npx babel --no-babelrc script.js --out-file script-compiled.js --presets=es2015,react

npx 用于“执行npm包二进制文件”