搜尋

反應搖滾

Jan 03, 2025 pm 10:55 PM

React Rock

反應搖滾

React-Rock 是一個輕量級包,用於管理 React 應用程式中的全域狀態。它透過提供包含行和元資料的儲存來簡化資料處理,同時提供執行 CRUD 操作等的方法。它可以輕鬆地與 React 元件集成,使其成為管理大型應用程式中複雜狀態的理想解決方案。

安裝

要安裝 React-Rock 軟體包,請在專案中執行以下命令:

npm install react-rock

特徵

  • 全域儲存管理:管理全域儲存中的行和元資料。
  • CRUD 操作:對行執行建立、讀取、更新和刪除操作。
  • 元管理:設定、取得和刪除元資料。
  • 最佳化重新渲染:使用凍結選項控制元件重新渲染。
  • 類別元件支援:使用 StoreComponent 將商店資料整合到類別元件中。

基本範例:建立商店並新增記錄

要建立新商店並新增記錄,請使用 createStore 函數。這是一個例子:

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 });
</rowtype>

行類型解釋

當建立一行時,它將具有以下屬性:

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
}
</row>

每一行將包含原始資料(Row)和一些附加屬性,如 _id、_index 和 _observe。

方法

這是一個包含所有可用方法及其描述的表格:

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.

find 方法的範例

find 方法可讓您根據特定條件搜尋商店中的行:

npm install react-rock

React 元件中的重新渲染

React-Rock 透過提供凍結機制來最佳化重新渲染。當發生儲存更新並且啟用凍結選項時,使用 find 或 findFirst 等方法存取儲存的 React 元件將不會自動重新渲染。這使您可以控制元件何時重新渲染,從而提高大型應用程式的效能。

位置類型

WhereType 用於指定查詢行時的條件。它定義了用於過濾行的查詢結構。

查詢值類型

QueryValueType 在WhereType 中使用來定義可能的查詢條件:

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.

WhereType 的範例

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 });
</rowtype>

參數類型

ArgsType 定義用於自訂查詢行為的選項,例如選擇特定行或跳過行。

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.

類別組件範例

要在類別元件中使用存儲,請擴充 StoreComponent 類別:

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 });
</rowtype>

尋找和查詢的範例

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
}
</row>

在多個組件中使用儲存的範例

React-Rock 允許您在多個組件之間共享同一個存儲,確保整個應用程式的狀態一致:

const foundUsers = users.find({ name: { equalWith: 'John Doe' } });
console.log(foundUsers);

類型說明

  • RowType:表示帶有 _id、_index 和 _observe 以及使用者定義的資料欄位的記錄。
  • ArgsType:定義彈性查詢行的選項,例如跳過、取得和自訂行處理。
  • WhereType:表示查詢記錄的條件,使用contain、equalWith等字段,以及gt、lt等範圍查詢
  • QueryValueType:指定根據欄位值過濾行所允許的條件類型。

執照

此軟體包已根據 MIT 許可證獲得許可。


本文檔應該提供如何有效使用 React-rock 套件的簡明概述。

?貢獻

歡迎貢獻!請查看貢獻指南。


?執照

該專案已獲得 MIT 許可。

以上是反應搖滾的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
JavaScript數據類型:瀏覽器和nodejs之間是否有區別?JavaScript數據類型:瀏覽器和nodejs之間是否有區別?May 14, 2025 am 12:15 AM

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScript評論:使用//和 / * * / * / * /JavaScript評論:使用//和 / * * / * / * /May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript:開發人員的比較分析Python vs. JavaScript:開發人員的比較分析May 09, 2025 am 12:22 AM

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

Python vs. JavaScript:選擇合適的工具Python vs. JavaScript:選擇合適的工具May 08, 2025 am 12:10 AM

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript:了解每個的優勢Python和JavaScript:了解每個的優勢May 06, 2025 am 12:15 AM

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

JavaScript的核心:它是在C還是C上構建的?JavaScript的核心:它是在C還是C上構建的?May 05, 2025 am 12:07 AM

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript應用程序:從前端到後端JavaScript應用程序:從前端到後端May 04, 2025 am 12:12 AM

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

Python vs. JavaScript:您應該學到哪種語言?Python vs. JavaScript:您應該學到哪種語言?May 03, 2025 am 12:10 AM

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用