React-Rock is a lightweight package for managing global state in React applications. It simplifies handling data by providing a store with rows and metadata, while offering methods to perform CRUD operations and more. It enables easy integration with React components, making it an ideal solution for managing complex state in large applications.
To install the React-Rock package, run the following command in your project:
npm install react-rock
To create a new store and add a record, use the createStore function. Here's an example:
import { createStore } from 'react-rock'; // Define RowType and MetaType type RowType = { name: string, age: number }; type MetaType = { totalRecords: number }; // Create a store const users = createStore<RowType, MetaType>({ name: '', age: 0 }, { totalRecords: 0 }); // Add a new row to the store users.create({ name: 'John Doe', age: 30 });
When a row is created, it will have the following properties:
type RowType<Row> = Row & { _id: string; // Unique identifier for the row _index: number; // Index of the row in the store _observe: number; // Internal property to track changes }
Each row will include the original data (Row) and some additional properties like _id, _index, and _observe.
Here’s a table with all available methods and their descriptions:
Method | Description |
---|---|
create(row, freeze?) | Adds a new record to the store. Optionally, prevents re-rendering if freeze is true. |
createMany(rows, freeze?) | Adds multiple records to the store. Optionally, prevents re-rendering if freeze is true. |
update(row, where, freeze?) | Updates records based on the condition specified in where. |
updateAll(row, freeze?) | Updates all records in the store. Optionally, prevents re-rendering if freeze is true. |
delete(where, freeze?) | Deletes records based on the condition specified in where. |
move(oldIdx, newIdx, freeze?) | Moves a record from one index to another. |
clearAll(freeze?) | Clears all records from the store. Optionally, prevents re-rendering if freeze is true. |
getAll(args?) | Retrieves all rows from the store. |
find(where, args?) | Finds rows based on a condition specified in where. |
findFirst(where, freeze?) | Finds the first row that matches the condition in where. |
findById(_id, freeze?) | Finds a row by its _id. |
setMeta(key, value, freeze?) | Sets a value for a specific meta key. |
getMeta(key, freeze?) | Retrieves the value of a specific meta key. |
getAllMeta(freeze?) | Retrieves all meta data from the store. |
deleteMeta(key, freeze?) | Deletes a specific meta key. |
clearMeta(freeze?) | Clears all meta data from the store. |
The find method allows you to search for rows in the store based on specific conditions:
npm install react-rock
React-Rock optimizes re-renders by offering a freeze mechanism. When a store update occurs and the freeze option is enabled, React components that access the store using methods like find or findFirst will not automatically re-render. This gives you control over when your components should re-render, improving performance in large applications.
The WhereType is used to specify conditions when querying rows. It defines a query structure for filtering rows.
The QueryValueType is used within WhereType to define possible conditions for querying:
Property | Description |
---|---|
contain | Finds values containing the specified string, number, or boolean. |
startWith | Finds values that start with the specified string or number. |
endWith | Finds values that end with the specified string or number. |
equalWith | Finds values that are exactly equal to the specified value. |
notEqualWith | Finds values that are not equal to the specified value. |
gt | Finds values greater than the specified number. |
lt | Finds values less than the specified number. |
gte | Finds values greater than or equal to the specified number. |
lte | Finds values less than or equal to the specified number. |
import { createStore } from 'react-rock'; // Define RowType and MetaType type RowType = { name: string, age: number }; type MetaType = { totalRecords: number }; // Create a store const users = createStore<RowType, MetaType>({ name: '', age: 0 }, { totalRecords: 0 }); // Add a new row to the store users.create({ name: 'John Doe', age: 30 });
The ArgsType defines options for customizing query behavior, such as selecting specific rows or skipping rows.
Property | Description |
---|---|
getRow | Custom function to process rows before returning them. |
skip | Number of rows to skip. |
take | Number of rows to return. |
freeze | If true, prevents re-rendering when accessing the data. |
To use the store in a class component, extend the StoreComponent class:
npm install react-rock
import { createStore } from 'react-rock'; // Define RowType and MetaType type RowType = { name: string, age: number }; type MetaType = { totalRecords: number }; // Create a store const users = createStore<RowType, MetaType>({ name: '', age: 0 }, { totalRecords: 0 }); // Add a new row to the store users.create({ name: 'John Doe', age: 30 });
type RowType<Row> = Row & { _id: string; // Unique identifier for the row _index: number; // Index of the row in the store _observe: number; // Internal property to track changes }
React-Rock allows you to share the same store across multiple components, ensuring a consistent state throughout the app:
const foundUsers = users.find({ name: { equalWith: 'John Doe' } }); console.log(foundUsers);
This package is licensed under the MIT License.
This documentation should provide a concise overview of how to use the react-rock package effectively.
Contributions are welcome! Please check out the contribution guidelines.
This project is licensed under the MIT License.
The above is the detailed content of React Rock. For more information, please follow other related articles on the PHP Chinese website!