gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:20:19 26 4
gpt4 key购买 nike

我正在尝试复制这个 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/

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