search
HomeWeb Front-endJS TutorialPastate.js responsive react state management framework

The content of this article is about the responsive react state management framework of Pastate.js. It has certain reference value. Friends in need can refer to it

Pastate Introduction

What is Pastate

Pastate is a responsive react state management framework that implements asynchronous responsive management of state. Pastate is a lean framework that provides friendly encapsulation of many advanced concepts, which means that you don't have to learn some difficult-to-understand concepts and can easily use paste in a simple application. As the application becomes increasingly complex, you only need to gradually learn and use more functions in paste to meet the increasingly complex system development needs. At the same time, pastate is also a backward-compatible progressive framework. You can use paste to implement some components in an existing react or redux project, and then gradually expand it to the entire project. Pastate Homepage: https://pastate.js.org

Pastate GitHub: https://github.com/BirdLeeSCUT/pastate (stars welcome)

Simple example :

const state = store.state

class AppView extends Component {
    increaseAge(){
        state.myInfo.age += 1
    }
    changeName(newName){
        state.myInfo.name = newName
    }
    render() {
        ...
    }
}

You can directly assign values ​​to the state node, and the responsive engine of pastate will update the view asynchronously for you! This mode is much more convenient and flexible than the setState method of native react or the reducer mode of native redux!

Features

  • Convenient and easy to use

    : pastate encapsulates advanced concepts, and you can easily get started as long as you have basic knowledge of react

  • Responsive state

    : You can directly use js native mode to update the state value, and pastate will update the relevant views for you in a responsive manner

  • Type Tips

    :pastate has a complete type definition file, which can greatly improve development efficiency with the help of intelliSense

  • On-demand rendering

    :pastate implementation With the on-demand traceability update reference mechanism of state nodes, the view can efficiently respond to changes in state

  • Asynchronous update mechanism

    : When you make continuous modifications to state, pastate Will efficiently do only one asynchronous view update for you

  • Friendly learning curve

    : pastate encapsulates the advanced concepts of flux mode, you only need to go when you are interested Understand these concepts

  • Compatible with redux projects

    : You can easily integrate pastate into redux projects and implement some containers as pastate mode

  • Support TypeScript

    : pastate itself is developed using TypeScript, with complete type definitions and generic support

  • ##MIT protocol authorization
  • : You can use it for free in personal or commercial projects, and modify or extend it as needed

    Principle Introduction
Pastate's name comes from the abbreviation of Path State, pastate Add the node's location (path) information and store ownership information to each node of the state, thereby realizing on-demand recursive reference updates of objects or array nodes, and realizing the immutable state feature, so the pastate can manage complex state trees and achieve efficient Asynchronous on-demand rendering. At the same time, pastate implements full-node responsive operation support for state based on ES5's Object.assign. You only need to modify the state like a normal variable, such as

state.userinfo.name = 'myName'

, this The responsive engine of pastate will automatically and asynchronously update the relevant views for you efficiently. For detailed principles, please see the principle chapter:

Pastate.js responsive react state management frameworkInspiration source

Pastate is inspired by the immutable state management model of redux and the responsive state design model of vue.js; it also incorporates lean design ideas.

Reliability

Pastate has passed comprehensive testing of 160 test cases and is stable and reliable. The specific content of the test cases can be found in the

src/pastate/tests

directory in the project source code.

Feedback Welcome

If you find bugs in paste or have any suggestions, please submit an issue. Thank you for your feedback. If you like pastete, I hope you can give it a valuable star

^_^

: pastete github.

Quick Start


Let’s take a look at how to use paste to build the simplest application.

Installation

Pastate is a react state management framework that needs to be used with react. We first use the create-react-app tool to create a basic react project, and demonstrate how to use pastate on this project:

$ npm install -g create-react-app
$ create-react-app my-pastate-app
$ cd my-pastate-app

Then, you can use npm to install pastate directly:

$ npm install --save pastate
or Install using yarn:

$ yarn add pastate
Get started

Pastate is very simple to use. Let's create a pastate component to display simple personal information.

Create

src/MyPastateApp.jsx

file to write our component:

import React, { Component } from 'react';
import { Pastore, makeOnlyContainer } from 'pastate';

const store = new Pastore({
    name: 'Peter',
    isBoy: true,
    age: 10
})

class AppView extends Component {
    render() {
        let state = store.state;
        return (
            <p>
                My name is {state.name}.<br/>
                I am a {state.isBoy ? "boy" : "girl"}.<br/>
                I am {state.age} years old.<br/>
            </p>
        )
    }
}

export default makeOnlyContainer(AppView, store)
Complete, this is an entry-level pastate component, there are the following two points that distinguish it from native react Project:

Component-independent
    store
  1. const store = new Pastore({
        name: &#39;Peter&#39;,
        isBoy: true,
        age: 10
    })

    Store is a
  2. data center
, It stores state data and contains a set of state management engines and view update engines.

在初始化 store 时,需要向 Pastore 构造函数里传入一个初始化 state, 我们通常使用以下命名的方式书写, 以便复用这个初始化 state:

const initState = {
    name: &#39;Peter&#39;,
    isBoy: true,
    age: 10
}
const store = new Pastore(initState)
  1. 对组件和 store 进行连接

对于只有唯一一个 store 的应用,我们使用 pastate 提供的 makeOnlyContainer 把 store 和组件(Component)连接成一个的容器, 这使得组件视图可以响应 store 中 state 的变化:

Pastate.js responsive react state management framework

接着,把该容器(Container)渲染在HTML中即可:

src/MyPastateApp.jsx

...
export default makeOnlyContainer(App, store)

src/index.js

import ReactDOM from &#39;react-dom&#39;;
import container from &#39;./MyPastateApp&#39;;
ReactDOM.render(container, document.getElementById(&#39;root&#39;));

注意,makeOnlyContainer 生成的是一个 React Element, 即 <xxx></xxx>, 因此在 render 时不必再多加一层 <...>

更新 state 值

接下来我们来尝试更新 state 的值:通过两个按钮来控制 state.age 值的增加和减少。

  • 先在组件中添加两个操作函数 increaseAgedecreaseAge

// src/MyPastateApp.jsx
...
const store = new Pastore(initState)
class AppView extends Component {

    increaseAge(){
        store.state.age += 1
    }
    
    decreaseAge(){
        store.state.age -= 1
    }

    render() {
        ...
    }
}
...

可以看到,使用 pastate 更新 state 非常简便:直接对 state 中需要更新的节点进行赋值即可,与 store 连接的视图会自动更新。

  • 接下来在 JSX 中添加两个按钮来触发这两个操作函数:

src/MyPastateApp.jsx

...
    render() {
        let state = store.state;
        return (
            <p>
            
                My name is {state.name}.<br/>
                I am a {state.isBoy ? "boy" : "girl"}.<br/>
                I am {state.age} years old.<br/>
                
                <button onClick={this.decreaseAge}> decrease age </button> 
                <button onClick={this.increaseAge}> increase age </button> 
                
            </p>
        )
    }
...

Amazing!我们第一个简单的 pastate 应用大功告成:

Pastate.js responsive react state management framework

点击 increaseAgedecreaseAge 按钮, 可以看到年龄值的变化。<br>你可以再添加几个按钮来修改 state 中名字和性别,看看视图有没有如你所愿地更新。

Pastate 在 store 中实现了一个响应式和 immutable 特性结合的 state 管理引擎, 我们可以像修改普通变量一样操作 state, 同时 pastate 可以高效地根据 state 的改变对相关视图进行更新。

编辑器智能提示(intelliSense)

我们推荐使用 Visual Studio Code 编辑器开发 react / pastate 应用,它拥有很好的变量类型智能提示功能和其他优秀特性,使得我们可以提高开发效率,并探测减少一些输入性错误。

Tips: vscode 默认关闭了通过 tab 键触发 emmet 的功能, 你可以通过修改设置开启: "emmet.triggerExpansionOnTab": true

下面我们简单地使用 jsDoc 注释来使 state 具有类型提示效果:<br>src/MyPastateApp.jsx

...
const initState = {
    name: &#39;Peter&#39;,
    isBoy: true,
    age: 10,
}
const store = new Pastore(initState)

/** @type {initState} */
const state = store.state; // 修改点, 把 state 提取成文件级的变量

class AppView extends Component {

    increaseAge(){
        state.age += 1 // 修改点,使用文件级的变量 state,下同
    }
    
    decreaseAge(){
        state.age -= 1 // 修改点
    }

    render() {
        // 修改点
        return (
            <p>
                My name is {state.name}.<br/> 
                I am a {state.isBoy ? "boy" : "girl"}.<br/>
                I am {state.age} years old.<br/>
                ... 
            </p>
        )
    }
}
...
  • 我们把 store.state 提取为文件级的变量 state,这使得对 state 的使用和修改变得方便。

  • 同时我们在 const state 之前加上类型注释 /** @type {initState} */, 使得编辑器知道 state 的格式,并获得如下的智能提示效果:

Pastate.js responsive react state management framework

智能提示的功能在 state 结构复杂的时候非常实用。

你也可以使用 pastate 提供的 createStore 函数来创建 store, 并自动获取 state 类型定义,具体用法请看API文档,我们现在先使用 new Pastore 的方式创建 store 。如果你使用 Typescript 进行开发,pastate 支持 Typescript 泛型的变量类型传递功能,无需使用 jsdoc 注释。

这里只是简单例子只是涉及到一个 react 组件,在文档中我们会介绍如何构建一个包含多个组件的 pastate 应用。


Pastate 文档

  • 1.快速上手

  • 2.多组件应用

  • 3.数组渲染与操作

  • 4.表单渲染与操作

  • 5.模块化

  • 6.多模块应用

  • 7.规模化

  • 8.原理与API文档

  • 9.其他资源

将持续更新,欢迎关注本专栏 ^_^

<br>

The above is the detailed content of Pastate.js responsive react state management framework. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools