Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Mar 11, 2021
0 parents commit 45a7a1a
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Fragmented store

When we update a property of the store, **only** the components that use that particular property **are rendered** even if they consume other elements of the store.

## Usage:

```js
import createStore from "fragmented-store";

const { Provider, useUsername, useAge } = createStore({
username: "Aral",
age: 31
});

export default function App() {
return (
<Provider>
<Title />
<UpdateTitle />
<Age />
</Provider>
);
}

function Title() {
const [username] = useUsername();

console.log("render Title");

return <h1>{username}</h1>;
}

function UpdateTitle() {
const [username, setUsername] = useUsername();

console.log("render UpdateTitle");

return (
<button onClick={() => setUsername((u) => u + "a")}>
Update {username}
</button>
);
}

function Age() {
const [age, setAge] = useAge();

console.log("render Age");

return (
<div>
<div>{age}</div>
<button onClick={() => setAge((a) => a + 1)}>Inc age</button>
</div>
);
}
```
38 changes: 38 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { useState, useContext, createContext } from "react";

export default function createStore(store = {}) {
const storeUtils = Object.keys(store).reduce((o, key) => {
const fieldCapitalized = `${key[0].toUpperCase()}${key.slice(
1,
key.length
)}`;
const context = createContext(store[key]);

return {
...o,
contexts: [...(o.contexts || []), { context, key }],
[`use${fieldCapitalized}`]: () => useContext(context)
};
}, {});

storeUtils.Provider = ({ children }) => {
const Empty = ({ children }) => children;
const Component = storeUtils.contexts
.map(({ context, key }) => ({ children }) => {
const ctx = useState(store[key]);
return <context.Provider value={ctx}>{children}</context.Provider>;
})
.reduce(
(RestProviders, Provider) => ({ children }) => (
<Provider>
<RestProviders>{children}</RestProviders>
</Provider>
),
Empty
);

return <Component>{children}</Component>;
};

return storeUtils;
}
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "fragmented-store",
"version": "0.1.0",
"description": "Small library to manage the (P)React state",
"source": "index.js",
"main": "dist/index.js",
"module": "dist/index.module.js",
"unpkg": "dist/index.umd.js",
"scripts": {
"build": "microbundle",
"dev": "microbundle watch",
"prepublish": "yarn build",
},
"keywords": [
"react",
"preact",
"state",
"state manager",
"javascript"
],
"author": {
"name": "Aral Roca Gòmez",
"email": "[email protected]"
},
"license": "MIT",
"peerDependencies": {
"react": ">= 16.8.0"
},
"devDependencies": {
"microbundle": "0.11.0",
"react": "16.13.1",
"react-dom": "16.13.1"
}
}

0 comments on commit 45a7a1a

Please sign in to comment.