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
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:Inspiration source
src/pastate/tests
directory in the project source code.Feedback Welcome
^_^
: pastete github.Quick Start
Let’s take a look at how to use paste to build the simplest application. InstallationPastate 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 pastateor Install using yarn:
$ yarn add pastateGet startedPastate 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
-
const store = new Pastore({ name: 'Peter', isBoy: true, age: 10 })
Store is a data center
在初始化 store 时,需要向 Pastore 构造函数里传入一个初始化 state, 我们通常使用以下命名的方式书写, 以便复用这个初始化 state:
const initState = { name: 'Peter', isBoy: true, age: 10 } const store = new Pastore(initState)
对组件和 store 进行连接
对于只有唯一一个 store 的应用,我们使用 pastate 提供的 makeOnlyContainer
把 store 和组件(Component)连接成一个的容器, 这使得组件视图可以响应 store 中 state 的变化:
接着,把该容器(Container)渲染在HTML中即可:
src/MyPastateApp.jsx
... export default makeOnlyContainer(App, store)
src/index.js
import ReactDOM from 'react-dom'; import container from './MyPastateApp'; ReactDOM.render(container, document.getElementById('root'));
注意,makeOnlyContainer
生成的是一个 React Element, 即 <xxx></xxx>
, 因此在 render 时不必再多加一层 <...>。
更新 state 值
接下来我们来尝试更新 state 的值:通过两个按钮来控制 state.age 值的增加和减少。
先在组件中添加两个操作函数
increaseAge
和decreaseAge
// 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 应用大功告成:
点击 increaseAge
和 decreaseAge
按钮, 可以看到年龄值的变化。<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: 'Peter', 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 的格式,并获得如下的智能提示效果:
智能提示的功能在 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!

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.

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 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.

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

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.

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.

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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
Powerful PHP integrated development environment

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
The latest (2018.2.1) professional PHP integrated development tool

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.
