# spreadem

> A simple package that uses spread operator to return a new copy of an object using ES6 spread operator. It is mostly compatible with older browsers.

Latest version **1.0.6** (published 2017-08-02) · WTFPL license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2017-08-02 |
| First published | 2017-08-02 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Niall O'Callaghan |
| Maintainers | noccer |
| Keywords | TypeScript, React, es5, es6, es2015, spread-operator |

## Links

- npm: https://www.npmjs.com/package/spreadem
- Repository: https://github.com/noccer/spreadem
- Homepage: https://github.com/noccer/spreadem#readme
- Issues: https://github.com/noccer/spreadem/issues
- npm.io page: https://npm.io/package/spreadem

## 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

- 1.0.6 (latest) — 2017-08-02
- 1.0.5 — 2017-08-02
- 1.0.4 — 2017-08-02
- 1.0.3 — 2017-08-02
- 1.0.2 — 2017-08-02
- 1.0.1 — 2017-08-02
- 1.0.0 — 2017-08-02

## README

# Spread'em

spread'em is a really small, relatively useless package that will simply take an object and return a new shallow copy using the spread operator.


### Usage

To run the example:

```
npm install --save spreadem
npm run example
```

The example will log out 2 objects - the original object and a new copy of the object that has been mutated. Note that the original object has not been altered.

Q: Why would you use this package?

> A: You probably shouldn't. It's dumb and you could easily whip up this functionality yourself.

Q: Then why did you make this package in the first place?
> A: The purpose of this package is simply to explore publishing a package to the mighty npm Registry. spreadem is not intended for production, use it entirely at your own risk. If you want to make a PR to this repo to make it better, go for it.

### Reason for creating this package

While using [React](https://facebook.github.io/react/), I have often seen people create what they believe to be a new 'copy' of state called `newState`, manipulate `newState`, then run `this.setstate(newState);`. In React, [this is a big no-no](https://facebook.github.io/react/docs/state-and-lifecycle.html#using-state-correctly), you are supposed to call `setState();`.

Little do they realise that they have now just mutated state directly, because by using `let newState = this.state;` we are actually creating a new reference to the original object, not creating a new object. This is a nuance of JavaScript, [more useful info can be found here](https://stackoverflow.com/questions/29050004/modifying-a-copy-of-a-javascript-object-is-causing-the-original-object-to-change).

Incorrect Example:

```
let newState = this.state; // newState is not brand new, it is just a reference to this.state.
newState.someProperty = "new value"; // Changing this.state changes newState and vice-versa. They are one and the same thing!
this.setState(newState); // pointless - you have already manipulated state.
// Do not pass Go, do not collect $200.
```

Naughty naughty! Doing the above is the same as the following:

```
this.state.someProperty = "new value"; // mutating state directly - oh the shame!
```

Remember, we are not supposed to mutate state directly ourselves. Instead we should pass an updated copy of state/key-value to `setState()`. From there, React queues up the state changes and asynchronously updates state in the most efficient way. Correct example:

```
let newState = {...this.state}; // make a new 'copy'
newState.someProperty = "new value"; // change the copy
this.setState(newState);
```

Using spread'em:

```
let newState = spreadem(this.state);
newState.someProperty = "new value";
this.setState(newState);
```

Under the hood, here's a simplified version of how spread'em works:

```
export const spreadem = (object) => {
    return {...object}; // yes it's really that simple
}
```

### Future functionality

I would like to increase the scope of spread'em to work with arrays as well:

```
export const spreadem = (array) => {
    return [...array];
}
```

### Todo list

- Upgrade to work with arrays.

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