Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@react-useful-hooks/use-mount

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-useful-hooks/use-mount

react hook and component that supports to easily handle different renders between server side and client side.

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

slack npm package PR WELCOME example status bundle size support


☀ use-mount

📌 Table of contents

  • 🔮 Preview
  • 🚧 Problem
  • 💡 Solution
  • 🌈 API
  • 📝 Example
  • 🏆 Contributors
  • 🚀 Contribution
  • 🧑‍💻 Issues
  • 🚧 License
  • 🦄 Sponsors

🔮 Preview

use-mount provide react hook and component to handle rendering either server side or client side.

Server SideClient Side
server-sideclient-side

🚧 Problem

Using SSR in react, there are always cicumstances to control server side rendering(SSR). And there are components like NoSSR. Although Nuxt.js support ClientOnly Component, Next.js does not support ClientOnly Component.

💡 Solution

useMount supports to easily handle server side rendering(SSR).
use-mount package not only provide react hook, which can easily manipulate to not rendering specific component in server side, but also support ClientOnly Component, which known as NoSSR Component but has more controls.

# yarn
yarn add @react-useful-hooks/use-mount
# npm
npm install @react-useful-hooks/use-mount

🌈 API

use-mount package provide useMount react hook and ClientOnly Component.

import { useMount, ClientOnly } from '@react-useful-hooks/use-mount';

useMount

type isMount = boolean;
type useMount = (callback?: () => void, deps?: any[]) => isMount;
useMount return type
returntypedescriptiondefault
isMountbooleanisMount value will turn true when hydration statredfalse
useMount param type
paramstypedescriptiondefault
callbackfunctioncallback will be called when hydration startedundefined
depsany[]any dependencies regarding callback function just like useEffectundefined

ClientOnly

type ClientOnly = ({
	children,
	fallback,
	fallbackTag,
	...delegates
}) => ReactElement;
ClientOnly props type
propstypedescriptiondefault
childrenReactElementchildren will be rendered when hydration startedundefined
fallbackstring or ReactElementfallback will be rendered at server sideundefined
fallbackTagkeyof JSX.IntrinsicElementscan switch parent tag such as h1, h2, ...etc'div'
delegatesany[]delagates are attributes for HTML tags such as label, aria-label, href, ...etcundefined

📝 Example

useMount

function Test() {
	const isMounted = useMount(() => {
		console.log('mounted!');
		return () => {
			console.log('unmounted!');
		};
	}, []);
}

ClinetOnly

1. Only rendering text
function Test() {
	/**
	 * @description
	 * This Component will render
	 * <div>useMount</div>
	 * in server side.
	 */
	return (
		<ClientOnly fallback={'useMount'}>
			<div>hello</div>
		</ClientOnly>
	);
}
2. Rendering with different tag
function Test() {
	/**
	 * @description
	 * This Component will render
	 * <h1>useMount</h1>
	 * in server side.
	 */
	return (
		<ClientOnly fallback="useMount" fallbackTag="h1">
			<div>hello</div>
		</ClientOnly>
	);
}

// also you can pass any atrributes to fallback tag
function Test() {
	/**
	 * @description
	 * This Component will render
	 * <h1 aria-label='hello'>useMount</h1>
	 * in server side.
	 */
	return (
		<ClientOnly fallback="useMount" fallbackTag="h1" aria-label="hello">
			<div>hello</div>
		</ClientOnly>
	);
}
3. Rendering Component
function ServerSide() {
	return <h1>useMount</h1>;
}

function Test() {
	/**
	 * @description
	 * This Component will render
	 * <h1>useMount</h1>
	 * in server side.
	 */
	return (
		<ClientOnly fallback={<ServerSide />}>
			<div>hello</div>
		</ClientOnly>
	);
}

🏆 Contributors

Thanks goes to these wonderful people.

eddie0329

🚀 Contribution

Looking to contribute? Look for the Good First Issue label.

Please make sure to read the Contributing Guide before making any pull requests.

🧑‍💻 Issues

🐛 Bugs

Please post issue for bugs, missing documetations, or unexpected behavior

Click for bug-report

⭐️ Feature Requests

Please post issue to suggest new features.

Click for featrure-request

🚧 License

MIT

🦄 Sponsors

Become first sponsor this project!

Check out more useful react hooks here.
If you have any questions, feel free to join our slack

Keywords

FAQs

Package last updated on 20 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc