My own understanding redux is similar to the Vuex in Vue, is a data manager, say, we start from the classic counter case to explain
Implementing counters using Redux
Create the following react project, I used to put each module block, only so many files, of course you can also write in a JS file, this is not the focus
First, let's look at the entry file for the project Index.js
Import ' Core-js/fn/object/a
returned when the call requests authorize, but the user triggers the LOGOUT action, the LOGOUT will be ignored and not processed, because loginFlow is blocked in authorize, It is not executed to take ('logout ').
Execute multiple tasks at the same time
If you need to execute multiple tasks at the same time in a specific scenario, such as requesting users data and products data, use the following method:
Import {call} from 'redux-saga/effects' // sync
property items, array type, of course, can be called other names.2,aitems's first argument state=[], why would you let state assign an array type? Shouldn't it be an object? This is because in assigning to Todos the sentence, items:aitems, I understand this point is when the trigger event, Redux will automatically traverse Todos, and then the state and Combinereducers in the object to compare, encountered the same name property, Pass the same name at
Brief Introduction
Redux is a very useful framework, and he mainly solves the problem of unified management of State. React, however, treats all components as state machines, so redux can be viewed as a react data management center. Note, however, that it is not necessary to manage data with Redux, but that the framework makes complex business logic and multiple
() () () () is available. AAA () returns a function, AAA () () returns a function, and AAA () () () finally executes.Let's take a look at how the middleware we're writing works. First, the use of middleware in the configstore inside the Applymiddleware inside the written.Applymiddleware (thunk, API)Let's look at the source code of redux:Node_modules/redux/src/applymiddleware.jsExportdefault functionapplymiddleware (... middlewares) {return(
the code for a middleware sample (from an official document):
/**
* Records all the initiated action and the resulting new state.
* *
Const LOGGER = store => next => action => {console.group
(action.type)
console.info (' Dispatching ', action ' let result
= Next (action)
Console.log (' Next state ', Store.getstate ())
Console.groupend (Action.type) return result
}
When I looked at the source code, I raised four main questions:1. Why do you want to pass in CreateStore2. What is args
Detailed description of the BIND instance of the applet Redux, the applet redux
Detailed description of Redux binding instances
Install
Clone or download the code library to your local device:
git clone https://github.com/charleyw/wechat-weapp-redux
Copy the dist/wechat-weapp-redux.js (or minify copy) file directly to
, getState}) { return next = action = > typeof action = = = ' function '? Action (Dispatch, GetState): Next (action);}When Thunkmiddleware determines that the action passed in is a function, the thunk function is given the dispatch and GetState parameters, otherwise, the next action is called, and the subsequent middleware (middleware Plug-ins can be bound multiple) to get the chance to use dispatch./* App/configurestore.js */import {compose,
put in the corresponding configuration file, such as routing, reducers and store configuration. Here I have placed them in a separate JS, only in the portal file through import, so management and maintenance will be very convenient, but will also increase the difficulty of understanding, but once you get started it will be easy. So let's take a look at the store configuration next.Store configurationImport thunk from ' Redux-thunk '//
React-redux Use summary React-redux store reducer Action integration storereduceraction supplement use Redux-dev-tools let change reducer to instantly Refresh page summary
Library Redux,react-redux,react-router-redux to use react
maps the bind to Props;mapdispatchtoprops to a props,connect to connect that component with Redux Since the state is gathered in the store, the values and modifications of the mapped props will all point to the store, which is resolved by provider;4, Provider1Import React from ' React ';2Import reactdom from ' React-dom ';3Import {CreateStore} from ' Redux ';4Im
, and then the processed app components thrown there, it's over. We mainly look at how its store is created, and how the app handles it.3. See how the store deals with it first. This is/store/configurestore.js.Import {Applymiddleware,createstore} from "Redux", import thunk from "Redux-thunk", import reducer from ". /reducers/reducer.js "const createstorewithmiddl
Redux simple usage, redux usage
I. ipvcers
It tells us what we can do. The returned result is a simple function, which requires two parameters: state and action. state is a public variable value, and action is used to determine what operations the type condition performs;
Ii. createStore
To create a store object, you must input the function returned by the c
state, please return a new copy; When you encounter an unknown action, you must return to the old state by default;
Also, please note that the {type: ' Red '} object above is semantically 4, store
The store is an object that is generated using the CreateStore method provided by Redux, and we need to pass reducer as a parameter, in this case:
Reducer
function Color (state, action) {
if (typeof state =
React-Redux and parse xreact-redux
I have been exploring React-related things before. I have a SPA project on hand, so I am going to try the water on Redux. Redux is not associated with React itself. It is a common Javscript App module used for App State management. To use Redux
In the previous chapter, we explained the CreateStore. Next, let's take a look at combinereducer.In Redux, we prohibit the creation of more than one store in the application (we are discussing the client application by default, and the homogeneous application does not apply to this rule).
However, as applications become more complex, reducer functions need to be split, and each piece of split is independent
The main introduction of the createstore,combinereducers,compose of the implementation of the principle, below, we look at the redux of the most interesting middleware part of the applymiddleware.Applymiddleware code is concise, but it has a broad meaning. Let's take a look at:
First, let's revisit the middleware approach: invoking middlewareTake a look at CreateStore's source code.
if (typeof preloadedstat
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.