javascript - onClick 处理程序未向 ReactDOMServer.renderToString 注册

标签 javascript html css node.js reactjs

我正在尝试复制这个 fiddle : http://jsfiddle.net/jhudson8/135oo6f8/

(我也试过这个例子 http://codepen.io/adamaoc/pen/wBGGQv 并且存在相同的 onClick 处理程序问题)

并使用 ReactDOMServer.renderToString

使 fiddle 为服务器端渲染工作

我有这个电话:

   res.send(ReactDOMServer.renderToString((
            <html>
            <head>

                <link href={'/styles/style-accordion.css'} rel={'stylesheet'} type={'text/css'}></link>

            </head>

            <body>


            <Accordion selected='2'>
                <AccordionSection title='Section 1' id='1'>
                    Section 1 content
                </AccordionSection>
                <AccordionSection title='Section 2' id='2'>
                    Section 2 content
                </AccordionSection>
                <AccordionSection title='Section 3' id='3'>
                    Section 3 content
                </AccordionSection>
            </Accordion>
            </body>
            </html>
        )));

Accordion 元素如下所示:

const React = require('react');

const fs = require('fs');
const path = require('path');


const Accordion = React.createClass({

    getInitialState: function () {
        // we should also listen for property changes and reset the state
        // but we aren't for this demo
        return {
            // initialize state with the selected section if provided
            selected: this.props.selected
        };
    },

    render: function () {

        // enhance the section contents so we can track clicks and show sections
        const children = React.Children.map(this.props.children, this.enhanceSection);

        return (
            <div className='accordion'>
                {children}
            </div>
        );
    },

    // return a cloned Section object with click tracking and 'active' awareness
    enhanceSection: function (child) {

        const selectedId = this.state.selected;
        const id = child.props.id;

        return React.cloneElement(child, {
            key: id,
            // private attributes/methods that the Section component works with
            _selected: id === selectedId,
            _onSelect: this.onSelect
        });
    },

    // when this section is selected, inform the parent Accordion component
    onSelect: function (id) {
        this.setState({selected: id});
    }
});


module.exports = Accordion;

AccordionSection 组件看起来像这样:

const React = require('react');


const AccordionSection = React.createClass({

    render: function () {

        const className = 'accordion-section' + (this.props._selected ? ' selected' : '');

        return (
            <div className={className}>
                <h3 onClick={this.onSelect}>
                    {this.props.title}
                </h3>
                <div className='body'>
                    {this.props.children}
                </div>
            </div>
        );
    },

    onSelect: function (e) {
        console.log('event:',e);
        // tell the parent Accordion component that this section was selected
        this.props._onSelect(this.props.id);
    }
});



module.exports = AccordionSection;

一切正常,CSS 正常工作,但问题是 onClick 没有注册。所以点击 Accordion 元素什么都不做。有谁知道为什么 onClick 处理程序在这种情况下可能无法注册?

最佳答案

React DOM render to string 仅将初始 HTML 作为字符串发送,没有任何 JS。
您还需要一个客户端 react 路由器,它将根据它们的 react-id 将所需的 JS 处理程序附加到 HTML。 JS需要两边都运行。
用于快速启动的通用渲染样板。 https://github.com/erikras/react-redux-universal-hot-example
另一个和你类似的问题。 React.js Serverside rendering and Event Handlers

关于javascript - onClick 处理程序未向 ReactDOMServer.renderToString 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36323336/

相关文章:

html - 我的样式表无法正常工作/链接不正确

html - 根据父总宽度剩余的div宽度自动

html - IE11 flexbox 最大宽度和边距 :auto;

javascript - 如何从异步调用返回响应?

javascript - JQuery/JavaScript : Iterate through form fields, 执行计算,然后提交

javascript - 为什么 d3 忽略我的颜色数组?

html - 如何使 div 中的 float 没有外边距

javascript - 如何找到最近的元素

html - HTML 格式的下载链接不适用于 exe

css - 行高降低的悬停文本的背景颜色隐藏了部分文本