Home >Web Front-end >JS Tutorial >Summary of some commonly used techniques in React (code)

Summary of some commonly used techniques in React (code)

不言
不言Original
2018-09-07 17:45:191618browse

This article brings you a summary (code) of some commonly used techniques in React. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. React-classnames library

In practical applications, it is often encountered to add or change classes in component properties based on certain states. In order to better meet the needs of dynamic class switching, React provides the classNames tool

安装:
npm install classnames --save
引入classnames库:
import classnames from 'classnames'

Usage:

1.基本使用

classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'

// lots of arguments of various types
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'

// other falsy values are just ignored
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'

2.也可以传入数组对象

var arr = ['b', { c: true, d: false }];
classNames('a', arr); // => 'a b c'

3.可以传入动态class

let buttonType = 'primary';
classNames({ [`btn-${buttonType}`]: true });

Reference gitHub

二.qs.parse (), qs.stringify() usage method

qs is a package managed by an npm warehouse (can be installed through the npm install qs command, it is already available in Dva, so there is no need to install it).

The reference is: import qs from 'qs';
1. qs.parse() parses the URL into the form of an object

import  Qs from 'qs';
let url = 'method=query_sql_dataset_data&projectId=85&appToken=7d22e38e-5717-11e7-907b-a6006ad3dba0';
Qs.parse(url);
console.log(Qs.parse(url));

As shown in the above code, the output result is as follows:

{
	method:'query_sql_dataset_data',
	projectId:'85',
	appToken:'7d22e38e-5717-11e7-907b-a6006ad3dba0'
}

2. qs.stringify() serializes the object into the form of a URL and concatenates it with &

import  Qs from 'qs';
let obj= {
     method: "query_sql_dataset_data",
     projectId: "85",
     appToken: "7d22e38e-5717-11e7-907b-a6006ad3dba0",
     datasetId: " 12564701"
   };
Qs.stringify(obj);
console.log(Qs.stringify(obj));

The output is:

method=query_sql_dataset_data&projectId=85&appToken=7d22e38e-5717- 11e7-907b-a6006ad3dba0&datasetId= 564701

It should be noted here that the stringify method also exists in JSON, but the difference between the two is obvious, as shown below:

{"uid":"cs11","pwd":"000000als","username":"cs11","password":"000000als"}
uid=cs11&pwd=000000als&username=cs11&password=000000als

As shown above, the former is processed by JSON.stringify(param), and the latter is processed by Qs.stringify(param).

3. Quote Alibaba font library Iconfont

(1). Alibaba font library Iconfont--find the pictures you need--download to local

(2).Quote The font image

Summary of some commonly used techniques in React (code)

is quoted in index.html in public:

Use

in the component. Note: Although the above reference can be displayed, it will Error (Invalid DOM property `class`. Did you mean `className`?) So currently it can only be downloaded as an icon and then quoted

constructor(props) {
	super(props);
	this.state = {
		"userImg": require('../../assets/img/user.png'),
		"address": require('../../assets/img/address.svg'),
	};
}
<img  src={this.state.userImg}/ alt="Summary of some commonly used techniques in React (code)" >

Related recommendations:

React High Summary of skills for using stage components

Summary of common PHP skills, PHP summary

The above is the detailed content of Summary of some commonly used techniques in React (code). 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