# stateful-react-container-webpack-plugin

> A simple plugin that adds a React container element with initial state from the server to a template built with HtmlWebpackPlugin

Latest version **0.4.0** (published 2019-04-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install stateful-react-container-webpack-plugin
pnpm add stateful-react-container-webpack-plugin
yarn add stateful-react-container-webpack-plugin
bun add stateful-react-container-webpack-plugin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2019-04-18 |
| First published | 2016-06-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Haukur Páll Hallvarðsson |
| Maintainers | haukur |
| Keywords | webpack, react, state, html-webpack-plugin |

## Links

- npm: https://www.npmjs.com/package/stateful-react-container-webpack-plugin
- Repository: https://github.com/hph/stateful-react-container-webpack-plugin
- Issues: https://github.com/hph/stateful-react-container-webpack-plugin/issues
- npm.io page: https://npm.io/package/stateful-react-container-webpack-plugin

## Dependencies (1)

- [lodash.defaults](https://npm.io/package/lodash.defaults.md) 4.0.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.4.0 (latest) — 2019-04-18
- 0.3.2 — 2017-08-11
- 0.3.1 — 2017-08-11
- 0.3.0 — 2017-08-11
- 0.2.1 — 2017-06-04
- 0.2.0 — 2017-06-04
- 0.1.1 — 2016-06-17
- 0.1.0 — 2016-06-17

## README

# stateful-react-container-webpack-plugin

This [Webpack](https://webpack.github.io/) plugin in intended to be used with
[html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin) in
projects using React.js. This plugin allows you to use html-webpack-plugin to
build templates automatically with Webpack while also server-side rendering and
passing initial state to the React.js client-side app.

There may be an example of the usage over at [Kit](https://github.com/hph/kit)
when I've had time to update it. See the [Example output](#Example-output) to
get an idea what this does.

### Installation

    npm install -S stateful-react-container-webpack-plugin

### Setup

After installing the plugin, you will need to add it to the plugins in your
Webpack configuration:

```javascript
plugins: [
  new HtmlWebpackPlugin(),
  new StatefulReactContainerPlugin(),
],
```

### Options

The plugin provides three options (passed as an object argument to the plugin)
to customize usage:

- `id` (default: `container`): The id of the React container.js element.
- `attribute` (default: `state`): The name of the data attribute to store the
  initial state for the React.js app.
- `variable` (default: `state`): The name of the variable containing the
  initial state in the template.
- `noState` (default: `false`): Omits the data attribute and its value. This is
  useful if you only want this plugin to generate the container element and are
  not using server rendering.
- `position` (default: `'start'`): Determines whether the container is
  immediately after the opening body tag or immediately before the closing tag.
  Can be either `'start'` or `'end'`.

### Example output

With the default configuration, the `<body>` tag is replaced with the following
markup:

```html
<body><div id="container" data-state="<%= state %>"></div>
```

### Notes

This plugin assumes that you're using `ejs` to render your templates on the
server side (hence the `<%= state %>` in the output). Using Express.js, you
might render the built template as follows:

```javascript
// in a request handler
const state = JSON.stringify({
  user: req.session.user,
});
res.render('app.ejs', { state });
```

---
_Source: https://npm.io/package/stateful-react-container-webpack-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
