# nion

> Redux is awesome. It's an extremely elegant solution to the central problem facing frontend web developers - how to manage complex application state. However, there are two fundamental issues with redux that have made themselves more and more apparent as

Latest version **1.0.0** (published 2018-03-02) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install nion
pnpm add nion
yarn add nion
bun add nion
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-03-02 |
| First published | 2018-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 21 |
| Unpacked size | 474.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | jbyttow |

## Links

- npm: https://www.npmjs.com/package/nion
- npm.io page: https://npm.io/package/nion

## Dependencies (21)

- [url](https://npm.io/package/url.md) ^0.11.0
- [humps](https://npm.io/package/humps.md) ^2.0.1
- [react](https://npm.io/package/react.md) ^16.0.0
- [redux](https://npm.io/package/redux.md) ^3.7.1
- [reselect](https://npm.io/package/reselect.md) ^3.0.1
- [deep-equal](https://npm.io/package/deep-equal.md) ^1.0.1
- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2
- [lodash.map](https://npm.io/package/lodash.map.md) ^4.6.0
- [lodash.set](https://npm.io/package/lodash.set.md) ^4.3.2
- [prop-types](https://npm.io/package/prop-types.md) ^15.6.0
- [lodash.omit](https://npm.io/package/lodash.omit.md) ^4.5.0
- [react-redux](https://npm.io/package/react-redux.md) ^5.0.5
- [lodash.every](https://npm.io/package/lodash.every.md) ^4.6.0
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.0
- [lodash.union](https://npm.io/package/lodash.union.md) ^4.6.0
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^6.23.0
- [is-equal-shallow](https://npm.io/package/is-equal-shallow.md) ^0.1.3
- [isomorphic-fetch](https://npm.io/package/isomorphic-fetch.md) github:patreon/isomorphic-fetch#e4f90061be5fabed8fde118b04585478bfe185b2
- [lodash.mapvalues](https://npm.io/package/lodash.mapvalues.md) ^4.6.0
- [lodash.difference](https://npm.io/package/lodash.difference.md) ^4.5.0
- [seamless-immutable](https://npm.io/package/seamless-immutable.md) ^7.1.2

## Recent versions

- 1.0.0 (latest) — 2018-03-02

## README

# nion

Redux is awesome. It's an extremely elegant solution to the central problem facing frontend web developers - how to manage complex application state. However, there are two fundamental issues with redux that have made themselves more and more apparent as both applications and development teams grow.

##### Problem 1: Data Fetching
Data Fetching is hard. Managing network requests and the data that comes back is hard enough, but doing it using redux can be downright maddening. This is because redux, at it's core, is a system that relies on **pure functions** - functions that always return the same thing given the same input. By their very nature, network requests are **impure** - they can return errors, for instance. In order to accommodate these **side effects**, the redux ecosystem has come up with a [number](https://github.com/agraboso/redux-api-middleware) of [solutions](https://github.com/yelouafi/redux-saga). However, using these tools can be anything but straightforward due to tricky syntax, complexity, and lack of community consensus.

##### Problem 2: Conventions / Patterns
Since redux is so low level, much of an app's redux implementation is left up to the developer. While certain practices have become *de rigeur*, there really isn't anything close to an opinionated framework that developers can use as a guide. Because of this, setting and enforcing code conventions and patterns become **extremely** important as an application and team grows.

### Thus, nion
nion is our best attempt at addressing the main issues that come with developing a complex redux web application. It's an **opinionated** solution to the problems we faced building a React web application **at Patreon**. nion was designed with the specific purpose of making the development experience **predictable**, **clear**, and **consistent**. nion always emphasizes the pragmatic solution over the theoretical, and aims to expose the minimum surface area possible to the developer. In short, nion aims to **simplify** the process of managing data in a React app.

nion is heavily inspired by [Apollo](http://www.apollodata.com/http://www.apollodata.com/) and [GraphQL](http://graphql.org/).

## How it works

nion is best used as a **decorator function** which declares what data will be managed by the decorated component and passes in props for managing that data.

```javascript
@nion({
    currentUser: {
        endpoint: buildUrl('/current_user'),
    }
})
class UserContainer extends Component {
    render() {
        const { currentUser } = this.props.nion
        const { request, actions, data } = currentUser

        const loadButton = <Button onClick={() => actions.get()}>Load</Button>

        return (
            <Card>
                { request.isLoading ? <LoadingSpinner /> : loadButton }
                { exists(currentUser.data) ? <UserCard user={currentUser.data} /> : null }
            </Card>
        )
    }
}
```

We simply pass in an object with a special `declaration` that tells nion **what** to fetch, and nion automatically handles fetching the data and passing both it and the corresponding request status in as props to the decorated component.

nion significantly reduces the complexity of managing data by both abstracting away all of the logic and code needed to select data and handle requests, and by offering a clear and consistent pattern for doing so. As we'll see later, nion offers simple solutions for nearly every type of common application scenario, including component/request lifecycles, updates, multiple requests, and pagination.

The central component to understanding nion is the `dataKey`. In the above example, the `dataKey` is `"currentUser"`. The `dataKey` is the address on the state tree with which nion manages a given resource. A resource is composed of both the underlying data and corresponding network request status for a given piece of application state.

Let's take a look at what the corresponding redux state tree  looks like (after the data has been fetched) to better understand what nion is doing under the hood.

```javascript
nion: {
    entities: {
        user: {
            '3803025': {
                attributes: {...},
                relationships: {},
            }
        }
    },
    references: {
        currentUser: {
            entities: [{
                type: 'user',
                id: '3803025'
            }],
            isCollection: false,
    },
        requests: {
            currentUser: {
                fetchedAt: 1480617638990,
                isError: false,
                isLoaded: true,
                isLoading: false,
                status: 'success',
            }
        }
    }
}
```

nion manages three internal reducers that handle data fetching and management across the application:

##### Entities
The entities reducer keeps a **normalized** map of all given entities in the system, keyed by `type` and `id`. This means that all data is kept consistent regardless of where it's being accessed from.

##### References
The references reducer maintains a map of `dataKeys` pointing to the corresponding entities (as an object with `type` and `id` fields).

##### Requests
The requests reducer maintains a map of `dataKeys` that tracks all network request details around fetching / updating data.

Internally, the nion suite of redux tools handles everything necessary to maintain application state and provide a clear interface to the higher-level component tooling.

### Get started
Learn more in the [guide](docs/getting-started.md)

### Learn More

[examples](docs/examples.md)

[deep dive](docs/deep-dive.md)

[api](docs/api.md)

[extensions](docs/extensions.md)

[api-modules](docs/api-modules.md)

[nion-core](docs/core.md)

[definitions](docs/definitions.md)

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