加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 服务器端渲染在客户端重新渲染

发布时间:2020-12-15 05:07:04 所属栏目:百科 来源:网络整理
导读:我试图用 django,express,react和react-router-dom实现服务器端渲染. server.js const html = renderToString(( Provider store={store} StaticRouter basename={baseUrl} location={location} context={context} AppRoute / /StaticRouter /Provider ),null
我试图用 django,express,react和react-router-dom实现服务器端渲染.

server.js

const html = renderToString((
    <Provider store={store}>
      <StaticRouter basename={baseUrl} location={location} context={context}>
        <AppRoute />
      </StaticRouter>
    </Provider>
  ),null,2);

  const preloadedState = store.getState();

  res.send(renderFullPage(html,preloadedState))

client.js的index.js

ReactDOM.render(
  <Provider store={store}>
    <Router>
      <AppRoute />
    </Router>
  </Provider>,document.getElementById('app')
);

AppRoute为客户端使用RouteAsync,为服务器使用RouteSync

AppRoute.js导入

import {App} from './RouteSync'
import {CollegeList} from './RouteSync'
import {CollegeDetail} from './RouteSync'

RouteAsync.js

export const App = asyncRoute(() => System.import('../app'));
export const CollegeList = asyncRoute(() => System.import('../apps/college/CollegeList'));
export const CollegeDetail = asyncRoute(() => System.import('../apps/college/CollegeDetail'));

RouteSync.js

export { default as App } from '../app'
export { default as CollegeList } from '../apps/college/CollegeList'
export { default as CollegeDetail } from '../apps/college/CollegeDetail'

webpack NormalModuleReplacementPlugin将RouteSync更改为RouteAsync

new webpack.NormalModuleReplacementPlugin(
      /./RouteSync$/,'./RouteAsync'
    ),

服务器渲染完成并发送给客户端.在客户端之后,SSR客户端在从devtools性能查看时再次重新呈现整个页面.

devtools “performance” image showing blank page before client re-render

我希望react只会挂钩事件监听器,而不是渲染页面.

任何建议停止在客户端重新呈现.

我也没有找到答案.并创造了我自己的首发.它使用react-router v4,redux,redux-saga.为防止在客户端上为第一个链接重新渲染,请使用水合物方法.我已经基于react-router v4和redux的官方文档和建议创建了这个.希望能帮助到你.

https://github.com/gzoreslav/react-redux-saga-universal-application

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读