1 Star 0 Fork 0

KwooShung / react-no-ssr

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

React NoSSR

这是一个名为NoSSR的React组件,它的主要功能是在服务器端渲染(SSR)环境中阻止其子组件的渲染。这个组件在客户端环境中(如浏览器)正常渲染其子组件,但在服务器端渲染环境中,它不会渲染任何内容。

GitHub License GitHub Release Date - Published_At GitHub last commit GitHub code size in bytes GitHub top language GitHub pull requests GitHub issues Github Stars NPM Version Npm.js Downloads/Week Github CI/CD codecov Maintainability Gitee Repo

English | 中文

为什么开发它?

  • 客户端特定代码:有些代码只能在浏览器环境中运行,例如,依赖于window、document或其他浏览器API的代码。在服务器端渲染(SSR)环境中运行这些代码会导致错误。NoSSR组件可以阻止这些代码在服务器端渲染,确保它们只在客户端环境中运行。
  • 性能优化:在服务器端渲染大量的组件或复杂的组件可能会导致服务器负载过大,影响性能。使用NoSSR组件,你可以选择只在客户端渲染一些非关键的或资源密集型的组件,从而减轻服务器负载,提高性能。
  • 用户体验:有些组件在服务器端渲染后可能需要在客户端再次渲染,例如,依赖于用户交互或浏览器尺寸的组件。这可能会导致页面闪烁或布局抖动,影响用户体验。使用NoSSR组件,你可以阻止这些组件在服务器端渲染,避免页面闪烁或布局抖动。

安装

npm

npm install @kwooshung/react-no-ssr

yarn

yarn add @kwooshung/react-no-ssr

pnpm

pnpm add @kwooshung/react-no-ssr

使用方法

组件

import { NoSSR } from '@kwooshung/react-no-ssr';

function App() {
  return <NoSSR>这里的内容,只会在浏览器中显示</NoSSR>;
}

此组件,只有一个可选属性:Loading,在你的组件等待异步操作完成时,React 会渲染 loading。这通常被设置为一个加载指示器,例如一个旋转的图标或者"加载中..."的文本,以给用户一个正在加载的反馈。当异步操作完成,loading 将被替换为你的组件的子组件。

import { NoSSR } from '@kwooshung/react-no-ssr';

function App() {
  return <NoSSR loading={<div>loading...</div>}>这里的内容,只会在浏览器中显示</NoSSR>;
}

hooks

useNoSSR 它接受一个回调函数作为参数,这个回调函数只会在浏览器中执行,而不会在服务器端执行。

import { useNoSSR } from '@kwooshung/react-no-ssr';

function App() {
  useNoSSR(() => {
    console.log('这里的内容,在浏览器开发者工具中显示');
  });

  return </>;
}

export default App;

许可证

MIT

MIT License Copyright (c) 2023 游水芦苇 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

NoSSR is a React component used to prevent the rendering of its child components in a server-side rendering (SSR) environment,ensuring that specific content is only displayed in the client environment 展开 收起
JavaScript 等 4 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/kwooshung/react-no-ssr.git
git@gitee.com:kwooshung/react-no-ssr.git
kwooshung
react-no-ssr
react-no-ssr
develop

搜索帮助