npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-ssr-side-effects

v1.0.2

Published

Thread safe fork of react-side-effect

Downloads

62

Readme

React SSR Side Effects Downloads npm version

Thread safe fork of react-side-effect. Can be used safely during Server Side Rendering.

Key Features

  • Thread-Safe SSR: Each request gets its own state instance during SSR, ensuring that side effects are isolated between different SSR requests.
  • Client-Side Consistency: On the client side, behavior remains consistent with the original react-side-effect package.
  • Provider-Based API: Easily integrate the library by wrapping your app in a provider.

Installation

npm install --save react-ssr-side-effects

As a script tag

Development

<script src="https://unpkg.com/react/umd/react.development.js" type="text/javascript"></script>
<script src="https://unpkg.com/react-side-effect/lib/index.esm.js" type="text/javascript"></script>

Production

<script src="https://unpkg.com/react/umd/react.production.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/react-side-effect/lib/index.esm.min.js" type="text/javascript"></script>

Use Cases

  • Everything react-side-effect can do.
  • Send customised response status and headers conditionally via React components during Server Side Rendering.
  • Extract any information from react components during render to be used in the current request cycle.

How It Works

The main difference between this package and the original react-side-effect is that it provides thread safety during Server Side Rendering. This means that each SSR request gets its own isolated state, preventing shared state pollution between concurrent requests.

Example

import { withSideEffect } from 'react-ssr-side-effects';

export function ResponseComponent(props) {
	// Component that will receive props.
	return (null);
}

function reducePropsToState(props) {
	// Logic to reduce all instance props to state

	// Return only last instance's props
	return props.at(-1);
}

function handleStateChangeOnClient(state) {
	// Handle state changes on client.
}

export const Response = withSideEffect(
	reducePropsToState,
	handleStateChangeOnClient,
)(ResponseComponent);

function Home() {
    return (
        <>
            <Response statusCode={200} />
            {
                // Home component code
            }
        </>
    )
}

function PageNotFound() {
    return (
        <>
            <Response statusCode={404} />
            {
                // 404 code
            }
        </>
    )
}
import { SsrProvider } from 'react-ssr-side-effects';


const context = { };

const jsx = (
    <SsrProvider context={context}>
        {
            // Application code here
        }
    </SsrProvider>
);


const html = renderToString(jsx);

console.log(context.state.statusCode) // Component state collected in context

API

withSideEffect: (reducePropsToState, handleStateChangeOnClient, [mapStateOnServer]) -> ReactComponent -> ReactComponent

A higher-order component that, when mounting, unmounting or receiving new props, calls reducePropsToState with props of each mounted instance. It is up to you to return some state aggregated from these props.

On the client, every time the returned component is (un)mounted or its props change, reducePropsToState will be called, and the recalculated state will be passed to handleStateChangeOnClient where you may use it to trigger a side effect.

On the server, handleStateChangeOnClient will not be called. You will be able to retrieve the current state in the context passed into the SsrProvider after a renderToString() call.

SsrContextProvider(context) -> ReactComponent

The provider that needs to be wrapped on your application code to collect state during instance rendering. On server, after renderToString() is called, context.state will hold the accumulated state.

Contributing

Contributions are welcome! If you'd like to contribute, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request describing the changes you've made.

Reporting Issues

If you encounter any bugs, have questions, or have suggestions for improvements, please open an issue on the GitHub repository.

Author

👤 Karan Raina [email protected]