Actual usage analysis of react-redux plug-in project
This time I will bring you an analysis of the actual use of the react-redux plug-in project. What are the precautions for the actual use of the react-redux plug-in project? The following is a practical case, let's take a look.
You can check out my simple introduction to redux first
react-redux introduction
react-redux is to use redux to develop react A plug-in used at the time. In addition, redux is not a react product. redux can also be used in vue and angular. Here is a brief explanation of how to use react-redux to develop react.
Description
This plug-in can make our redux code more concise and beautiful. I assume that you already have a react environment that can display hello world created using create-react-app, and have installed redux.
Note: If it was just created using create-react-app, you need to run npm run eject to pop up the personalized settings, so that you can customize the configuration.
Installation
npm i react-redux --save
Use
react-redux provides two important interfaces: Provider, connect, using With this plug-in, you can forget about react-redux’s subscribe and dispatch, they will no longer be needed.
Code structure
//index.js import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import registerServiceWorker from './registerServiceWorker'; import { createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import { reducer } from './index.redux'; import { Provider } from 'react-redux' const store = createStore(reducer, applyMiddleware(thunk)); ReactDOM.render( <provider> <app></app> </provider>, document.getElementById('root') ); registerServiceWorker();
The outermost layer of the application in Provider, pass the store to it, and use it only this time.
//app.js import React, { Component } from 'react'; import { addCreator, removeCreator, addAsync } from './index.redux'; import { connect } from 'react-redux'; class App extends Component { render() { return ( <p> </p><h1 id="现在有美女-this-props-num-个">现在有美女{this.props.num}个。</h1> <button>add</button> <button>remove</button> <button>addAsync</button> > ); } } //定义把state中哪个属性放到props中 function mapStateToProps(state) { return { num: state } } //定义把哪些方法放到props中 const actionCreators={ addCreator, removeCreator, addAsync }; //connect其实就是一个高阶组件,把app传进去,返回一个新的app组件 App = connect(mapStateToProps, actionCreators)(App); export default App;
connect is responsible for obtaining the parameters required by the component from the outside. After being defined through connect, the properties and methods placed in props can be obtained directly through this.props.
The following is the definition of reducer.
//react.redux.js const Add = 'addGirl', Remove = "removeGirl"; export function reducer(state = 0, action) { switch (action.type) { case Add: return state + 1; case Remove: return state - 1; default: return 10; } } export function addCreator() { return { type: Add }; } export function removeCreator() { return { type: Remove }; } export function addAsync() { return (dispatch, getState) => { setTimeout(function () { dispatch(addCreator()); }, 1000); } }
Use decorators to write Conect more elegantly
First you need to install babel-plugin-transform-decorators-legacy and configure it in package.json.
Installation
npm i babel-plugin-transform-decorators-legacy --save-dev This is only for development use, so install it to --save-dev
Configuration
Configure the plugins attribute of babel
"babel": { "presets": [ "react-app" ], "plugins": [ ["transform-decorators-legacy"] ] }
Modify the original writing method
Use @connect to redefine it and write it above the class.
//App.js @connect((state) => ({ num: state }),{ addCreator, removeCreator, addAsync }) class App extends Component { .....//省略 // function mapStateToProps(state) { // return { num: state } // } // App = connect(mapStateToProps, { addCreator, removeCreator, addAsync })(App);
Solution to the VS Code decorator prompt "experimentalDecorators"
Click the configuration button in the lower left corner of Visual Studio Code (or File>Preferences>Configuration, Windows environment), open the user settings window, enter "experimentalDecorators" in the search box, and find that you can find the option, as follows:
"javascript.implicitProjectConfig.experimentalDecorators": false
Just change it to true.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to operate Vue to make a proxy agent
Use jquery to get the specific content of the uploaded file
The above is the detailed content of Actual usage analysis of react-redux plug-in project. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

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

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.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
