Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
simplest-react-store
Advanced tools
Readme
Simplest react store that does the job.
This package was originally written for fun some years ago.
Lightweight and easy-to-use state management solution for React applications. It provides a simple API for creating and managing stores, making state management more approachable and less cumbersome. With this package, developers can quickly set up and utilize state management in their React projects without the need for complex setups or extensive boilerplate code.
The key features of Simplest-React-Store include:
Minimalist Approach: The package embraces simplicity and aims to provide a straightforward solution for state management in React applications.
Small Footprint: Simplest-React-Store is designed to be lightweight and has a minimal impact on bundle size, ensuring optimal performance for applications.
Intuitive API: The API is intuitive and easy to understand, making it accessible for developers of all skill levels.
Using npm:
npm install simplest-react-store
Using yarn:
yarn add simplest-react-store
// store.ts
import { createStore } from 'simplest-react-store';
// Define the initial state of the store. This includes the properties isHelloShown, helloText, and user with their initial values.
const initialState = {
isHelloShown: false,
helloText: "",
user: {
name: "Matt",
age: 40,
},
};
export type State = typeof initialState;
// Define the actions that can be performed on the store.
// These actions are functions that receive the current state and
// any necessary parameters, and they update the state accordingly.
const actions = {
showHello: (state: State, toggle: boolean) => (state.isHelloShown = toggle),
setHelloText: (state: State, value: string) => (state.helloText = value),
setUser(
state: State,
newUserData: Partial<State["user"]>
) {
// For objects, it works similar to a standard reducer and is particularly useful for handling nested objects
// Update the user state by merging newUserData with the current user object
return { user: { ...state.user, ...newUserData } };
},
};
// Create the store by calling the createStore function and passing the initial state and actions.
export const { useStore, useStoreProp, Provider } = createStore(
initialState,
actions
);
Wrap your React application with the Provider component provided by the store. This makes the store available to all components in the application.
// _app.tsx
import { Provider as MyStoreProvider } from "./store";
function App({ Component, pageProps }: AppProps) {
return (
<MyStoreProvider>
/* Your app code */
</MyStoreProvider>
);
}
Access the store's state and actions in your components using the useStore and useStoreProp hooks.
// MyComponent.tsx
import { useEffect } from 'react';
import { useStoreProp } from "store";
export default function MyComponent() {
const [helloText] = useStoreProp("helloText");
const [user] = useStoreProp("user");
const [showHello, dispatch] = useStoreProp("isHelloShown");
useEffect(() => {
// You can use same dispatch for all actions as it is the same store
dispatch.setUser({name: "David", age: 19});
dispatch.setHelloText("Hello");
dispatch.showHello(true);
}, [dispatch]);
return (
<div>
{showHello && (
<span>
{helloText} {user.name}
</span>
)}
</div>
);
}
That's it! Now you can use the state properties and actions from the store in your components to manage and update the state of your application. Remember to import the necessary functions and components from the store.ts file where needed.
If you have any idea for an improvement, please file an issue. Feel free to make a PR if you are willing to collaborate on the project. Thank you :)
FAQs
[npm-url]: https://npmjs.org/package/simplest-react-store [npm-image]: http://img.shields.io/npm/v/simplest-react-store.svg
The npm package simplest-react-store receives a total of 0 weekly downloads. As such, simplest-react-store popularity was classified as not popular.
We found that simplest-react-store demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.