1.0.6 • Published 10 months ago

react-ext-state v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

React ext state

A simple package for using external states

Example

Install

npm i react-ext-state

Examples:

useState

// App.js
import "./App.css";

import Info from "./Info";
import Button from "./Button";

const App = () => {	
	return(<div className="app">
		<Info nameState="test"/>
		<Button title="Hello world!" nameState="test"/>
		<Button title="Easy ext state!" nameState="test"/>
	</div>)
};

export default App;

// Button.js
import Ext from "react-ext-state";

const Button = ({title, nameState}) => {
	const handle = () => {
		Ext.setState(nameState, title);
	};

	return(<button onClick={handle}>{title}</button>);
};

export default Button;


// Info.js
import Ext from "react-ext-state";

const Info = ({nameState}) => {
	const [info] = Ext.useState(nameState, 'Any inital value');
	return(<div className="info">{info}</div>);
};

export default Info;

useEvent

// Info.js
import React from "react";
import Ext from "react-ext-state";

const Info = ({nameState}) => {
	const [info, setInfo] = React.useState('Any inital value');
	Ext.useEvent(nameState, setInfo);
	return(<div className="info">{info}</div>);
};

export default Info;

Or

Isolated class style

// store.js
import Ext from "react-ext-state";

export default new class store extends Ext {
	state = {
		info: 'Any inital value'
	}

	set = (nameState, title) => {
		this.setState(nameState, title)
	}
}

// Button.js
import store from "./store.js";

const Button = ({title, nameState}) => {
	const handle = () => {
		store.set(nameState, title);
	};

	return(<button onClick={handle}>{title}</button>);
};

export default Button;

// Info.js
import store from "./store.js";

const Info = ({nameState}) => {
	const [info] = store.useState(nameState);
	return(<div className="info">{info}</div>);
};

export default Info;
1.0.6

10 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago