gpt4 book ai didi

javascript - react .js : handleSubmit or onClick not triggered when rendered on the server side

转载 作者:搜寻专家 更新时间:2023-11-01 00:18:14 25 4
gpt4 key购买 nike

我在服务器端呈现所有内容时遇到问题,并且触发了 handleSubmit。这是源代码:

index.js:

module.exports = require('./app/server');

应用程序/服务器/index.js:

'use strict';

var env = process.env.NODE_ENV || 'development';
var express = require('express');
var http = require('http');
var app = express();

app.set('state', {});


// Inject Config
require('../config/server')[env](app, express);

// Inject component rendering
require('node-jsx').install({extension: '.jsx'});
app.use(require('../../lib/renderReactComponent')(app));

// Start server
var server = http.createServer(app);
return server.listen(app.get('port'), function() {
return console.log('Listening on port ' + app.get('port') + ', Env: ' + app.settings.env);
});

module.exports = app;

lib/renderReactComponent.js:

var ReactApp = require('../app');
var React = require('react');
var path = require('path');
var url = require('url');

module.exports = function(app) {
return function(req, res, next) {
try {
var path = url.parse(req.url).pathname;
res.send(React.renderComponentToStaticMarkup(ReactApp({path: path, state: app.get('state')})));
} catch(err) {
return next(err)
}
}
}

应用程序/index.js:

var ReactApp = require('./components/app');
module.exports = ReactApp;

应用程序/组件/app.js:

/**
* @jsx React.DOM
*/

var React = require('react');
var ReactRouter = require('react-router-component');

var App = React.createClass({

propTypes: {state: React.PropTypes.object.isRequired},

getInitialState: function() {
return {items: [], text: ''};
},

handleSubmit: function () {
console.log("handleSubmit triggered!");
var $author = this.refs.author.getDOMNode(),
$text = this.refs.text.getDOMNode(),
author = $author.value.trim(),
text = $text.value.trim();

if(!author || !text) {
return false;
}

if(this.props.onCommentSubmit) {
this.props.onCommentSubmit({ author: author, text: text });
}

$author.value = '';
$text.value = '';

return false;
},

render: function() {
return (
<html>
<form className="commentForm" onSubmit={this.handleSubmit}>
<input type="text" ref="author" placeholder="Your name"/>
<input type="text" ref="text" placeholder="Say Something ..."/>
<input type="submit" value="Post"/>
</form>
</html>
);
}
});
module.exports = App;

这段代码没有给我报错,但是没有触发 handleSubmit。我认为这是因为它不是由服务器端呈现的。如何在尝试从服务器端呈现页面时触发表单事件 handleSubmit?

最佳答案

流程是这样的:

  • 客户请求页面
  • 服务器执行 React.renderComponentToString 并将 html 返回给客户端
  • 此时服务器停止做任何事情
  • 页面在客户端加载,并运行 React.renderComponent
  • React 注意到带有 reactid 的标记
    • 验证校验和
    • 如果一切顺利,只需添加事件监听器并等待事情发生
  • 用户可能会提交表单,调用您的 handleSubmit

在 handleSubmit 中,如果您想向服务器发送一些数据,您可以使用普通的 AJAX/WebSockets/等技术来完成。

查看 superagent用于在浏览器和 Node 中运行的良好 http 客户端。

关于javascript - react .js : handleSubmit or onClick not triggered when rendered on the server side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26150715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com