diff --git a/README.md b/README.md index 05e80bc..829c810 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,200 @@ [![npm](https://img.shields.io/npm/v/usesync?color=red)](https://www.npmjs.com/package/usesync) A custom React hook to synchronize and share public state across different components on your React project. -*** \ No newline at end of file +*** + +## Table of Contents +- [Installation](#installation) +- [Usage](#usage) +- [Notes](#notes) +- [License](#license) + +## Installation: + +Install the hook from **npm**: +```bash +$ npm install usesync --save +``` + +Then simply require it: +```js +const { useSync, sync, storage } = require("usesync") +``` +or +```js +import useSync, { sync, storage } from "usesync" +``` + +## Usage: +### useSync(id: string) + +This is the hook that you will use across your React components, the function registers them under a specific ID you will give: +```js +const Component = () => { + useSync('hello') + return ( +
+ Hello World! +
+ ) +} +``` + +To synchronize multiple components simply call the hook inside them and give the same ID: +```js +const ComponentA = () => { + useSync('hello') + return ( +
+ Hello World! (A) +
+ ) +} +const ComponentB = () => { + useSync('hello') + return ( +
+ Hello World! (B) +
+ ) +} +``` + +You can use the hook on a component for multiple times with multiple IDs: +```js +const Component = () => { + useSync('id num 1') + useSync('id num 2') + return ( +
+ Hello World! +
+ ) +} +``` + +**Note:** it is possible to dynamically update a hook ID on a component with no issue. + +### sync(id: string) + +The hook alone does nothing, to synchronize components registered under a specific ID you need to call this function, it causes to re-render all of them, here is an example: +```js +import React from 'react' +import ReactDOM from 'react-dom' +import useSync, { sync } from "usesync" + +const ComponentA = () => { + useSync('Components') + return ( +
+ Random Number (A): {Math.random()} +
+ ) +} +const ComponentB = () => { + useSync('Components') + useSync('ComponentB') + return ( +
+ Random Number (B): {Math.random()} +
+ ) +} +const ComponentC = () => { + useSync('Components') + return ( +
+ Random Number (C): {Math.random()} +
+ ) +} +const App = () => { + const handleClick = () => { + sync('Components') + } + const handleClick2 = () => { + sync('ComponentB') + } + return ( +
+ + + + + + +
+ ) +} + +ReactDOM.render(, document.getElementById('app')) +``` +Try it on [CodePen](https://codepen.io/imrdjai/pen/zYKQzqw)! + +### storage: object + +This is just an extra object publicly available across your app can be used to store data, you can put states for your componenets there, here is an example: +```js +import React from 'react' +import ReactDOM from 'react-dom' +import useSync, { sync, storage } from "usesync" + +storage.randomNumber = Math.random() + +const ComponentA = () => { + useSync('Components') + return ( +
+ Random Number (A): {storage.randomNumber} +
+ ) +} +const ComponentB = () => { + useSync('Components') + useSync('ComponentB') + return ( +
+ Random Number (B): {storage.randomNumber} +
+ ) +} +const ComponentC = () => { + useSync('Components') + return ( +
+ Random Number (C): {storage.randomNumber} +
+ ) +} +const App = () => { + const handleClick = () => { + storage.randomNumber = Math.random() + sync('Components') + } + const handleClick2 = () => { + storage.randomNumber = Math.random() + sync('ComponentB') + } + return ( +
+ + + + + + +
+ ) +} + +ReactDOM.render(, document.getElementById('app')) +``` +Try it on [CodePen](https://codepen.io/imrdjai/pen/XWjwqxO)! + +## Notes +Give this cool project a star ⭐! I will appreciate it ❤ + +[![GitHub Repo stars](https://img.shields.io/github/stars/iMrDJAi/useSync?style=social)](https://github.com/iMrDJAi/useSync) + +## License +[MIT](https://github.com/iMrDJAi/useSync/blob/master/LICENSE) © [iMrDJAi](https://github.com/iMrDJAi) \ No newline at end of file diff --git a/package.json b/package.json index 66daaff..2a7b30d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "usesync", - "version": "1.0.0-alpha", + "version": "1.0.0", "description": "A custom React hook to synchronize and share public state across different components on your React project", "main": "src/useSync.js", "repository": {