A simple and easy-to-use context manager for React applications.
To install Simply Context in your project, run the following command:
npm install simply-context
First, import the SimplyProvider
component and the SimplyUseData
hook from the simply-context
package:
import { SimplyProvider, SimplyUseData } from "simply-context";
Then, wrap your root component with the simplyProvider component and pass in any initial state you would like to use:
const App = () => {
return (
<SimplyProvider initialState={{ username: "" }}>
{/* your app components here */}
</SimplyProvider>
);
};
Next, you can use the SimplyUseData hook anywhere in your app to access and modify the context data:
const LoginForm = () => {
const [username, setUsername] = SimplyUseData("username");
const usernameRef = React.useRef <HTMLInputElement>(null);
const handleLogin = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
// do something with the username
if (usernameRef.current) {
let name = usernameRef.current.value;
setUsername(name);
}
};
return (
<form onSubmit={handleLogin}>
<input name="username" type="text" value={username} ref={usernameRef} />
<button type="submit">Login</button>
</form>
);
};
The SimplyProvider
component is a higher-order component that provides the context data and setter functions to its children components.
initialState
(optional): an object that represents the initial state of the context data.
The SimplyUseData hook is a hook that allows you to access and modify the context data from anywhere in your app.
key
: a string that represents the key of the context data you would like to access.
An array containing the value of the context data at the given key and a function that allows you to modify the context data.
For more examples and use cases of Simply Context, check out the example folder in this repository.
Simply Context is released under the MIT license. See LICENSE for details.
Contributions are welcome! Please see CONTRIBUTING for details.
Simply Context is maintained by Mathieu Piton.