Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactDOM.hydrate会覆盖服务端渲染结果,如果注释,相关事件就无效。如何处理? #202

Open
Link-Kou opened this issue Nov 24, 2019 · 2 comments

Comments

@Link-Kou
Copy link

疑问:
ReactDOM.hydrate 会覆盖服务端渲染的结果,而服务端渲染的时候,会传递一些参数来完成页面渲染,而客户端不执行ReactDOM.hydrate,相关事件就无法触发,而只能引入Jq来完成事件绑定。

代码实例

export default class View extends React.Component<ViewProps> {

    static getPartial(props: IProps) {
        const {ctx} = props;
        return {
            html: <Login location={ctx.req.url} appType={props.appType} csrf={props.ctx.csrf}/>
        }
    }

    render() {
        const {html, helper} = this.props;
        return (
            <html>
            <head>
                <title>TypeScript</title>
                <script src={helper.asset('../resource/js/jquery-1.11.0.min.js')}/>
                <link rel="stylesheet" href={helper.asset('login.css')}/>
            </head>
            <body>
            <div id="loginView" dangerouslySetInnerHTML={{
                __html: html
            }}
            />
            </body>
            <script src={helper.asset('../resource/client/js/login.jq.js')}/>
            <script src={helper.asset('manifest.js')}/>
            <script src={helper.asset('login.js')}/>
            </html>
        );
    }
}

/**
 * 浏览器客户端运行
 * 包含事件
 */
if (__CLIENT__) {
    ReactDOM.hydrate(<Login/>, document.getElementById('loginView'));
}
@Link-Kou
Copy link
Author

@njugray 麻烦有时间,辛苦解答一下我的疑惑

@njugray
Copy link
Contributor

njugray commented Nov 25, 2019

参数你要通过 挂 window的方式,客户端拿到结果,hydrate一个一样的 <Login location={ctx.req.url} appType={props.appType} csrf={props.ctx.csrf}/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants