Accessibility

暂略

 

代码分割

import()

The best way to introduce code-splitting into your app is through the dynamic ​​import()​​syntax.

Before:

import { add } from './math';

console.log(add(16, 26));

After:

import("./math").then(math => {
console.log(math.add(16, 26));
});

React.lazy

Before:

import OtherComponent from './OtherComponent';

function MyComponent() {
return (
<div>
<OtherComponent />
</div>
);
}

After:

const OtherComponent = React.lazy(() => import('./OtherComponent'));

function MyComponent() {
return (
<div>
<OtherComponent />
</div>
);
}

基于路由的代码分割

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import React, { Suspense, lazy } from 'react';

const Home = lazy(() => import('./routes/Home'));
const About = lazy(() => import('./routes/About'));

const App = () => (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
</Switch>
</Suspense>
</Router>
);

上下文

Context 提供了一个组件树传递数据的方式

不使用Context的情况下

class App extends React.Component {
render() {
return <Toolbar theme="dark" />;
}
}

function Toolbar(props) {
// The Toolbar component must take an extra "theme" prop
// and pass it to the ThemedButton. This can become painful
// if every single button in the app needs to know the theme
// because it would have to be passed through all components.
return (
<div>
<ThemedButton theme={props.theme} />
</div>
);
}

class ThemedButton extends React.Component {
render() {
return <Button theme={this.props.theme} />;
}
}

而使用Context

// Context lets us pass a value deep into the component tree
// without explicitly threading it through every component.
// Create a context for the current theme (with "light" as the default).
const ThemeContext = React.createContext('light');

class App extends React.Component {
render() {
// Use a Provider to pass the current theme to the tree below.
// Any component can read it, no matter how deep it is.
// In this example, we're passing "dark" as the current value.
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
}

// A component in the middle doesn't have to
// pass the theme down explicitly anymore.
function Toolbar(props) {
return (
<div>
<ThemedButton />
</div>
);
}

class ThemedButton extends React.Component {
// Assign a contextType to read the current theme context.
// React will find the closest theme Provider above and use its value.
// In this example, the current theme is "dark".
static contextType = ThemeContext;
render() {
return <Button theme={this.context} />;
}
}

错误界限

暂略

 

Forwording Refs

暂略

 

Fragments

这跟 js 的创建碎片一个意思,只不过它用于JSX语法

render() {
return (
<React.Fragment>
<ChildA />
<ChildB />
<ChildC />
</React.Fragment>
);
}

高阶组件

高阶组件并不是一个组件,它就是一个函数,接受一个组件并且返回一个被包装过的新组件。

import React, { Component } from 'react';

function componentWrapper(WrappedComponent) {
class HOC extends Component {
constructor(props) {
super(props);
this.state = {
name: ''
}
}
componentWillMount() {
const name = localStorage.getItem('name');
this.setState({
name
});
}
render () {
const newProps = {
name: this.state.name
}
return (
<WrappedComponent {...this.props} {...newProps} />
)
}
}
return NewComponent;
}

class ShowName extends Component {
render() {
return (
<h1> { this.props.name }</h1>
)
}
}
const ShowNameWidthData = componentWrapper(ShowName);

与其他库的集成

class SomePlugin extends React.Component {
componentDidMount() {
this.$el = $(this.el);
this.$el.somePlugin();
}

componentWillUnmount() {
this.$el.somePlugin('destroy');
}

render() {
return <div ref={el => this.el = el} />;
}
}

 

深入JSX

暂略

 

性能优化

暂略

 

Portals

暂略

 

Refs 和 Dom

Refs提供了访问DOM节点或在render方法中创建的元素的方法。

什么时候使用Refs:

  • 管理焦点,文本选择,媒体播放
  • 触发命令动画
  • 与第三方库集成
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
// create a ref to store the textInput DOM element
this.textInput = React.createRef();
this.focusTextInput = this.focusTextInput.bind(this);
}

focusTextInput() {
// Explicitly focus the text input using the raw DOM API
// Note: we're accessing "current" to get the DOM node
this.textInput.current.focus();
}

render() {
// tell React that we want to associate the <input> ref
// with the `textInput` that we created in the constructor
return (
<div>
<input
type="text"
ref={this.textInput} />

<input
type="button"
value="Focus the text input"
onClick={this.focusTextInput}
/>
</div>
);
}
}