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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.