@abw/react-context-generator 中文文档教程

发布于 3年前 浏览 20 项目主页 更新于 3年前

react-context-generator

Introduction

这是一个小而简单的模块,提供了一些语法糖 简化 React Context 在 React 中维护状态的使用 应用程序。

说真的,它小得令人尴尬。 我们说的是 20 行 代码左右。

如果你还不熟悉 React Context 那么你应该从 阅读 React Context 文档:

Installation

https://reactjs.org/docs/context.html 使用 npmyarn

Either:

$ npm install --save @abw/react-context-generator

Or:

$ yarn add @abw/react-context-generator

Wrapping a Component that Maintains State

这是一个维护某些状态的组件的简单示例 并提供操纵该状态的方法。

// Counter.js
import React from "react";
import Generator from "@abw/react-context-generator";

class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: this.props.initialCount || 0
    };
    this.actions = {
      inc: this.increment.bind(this),
      dec: this.decrement.bind(this),
    };
  }
  increment() {
    this.setState({ count: this.state.count + 1 });
  }
  decrement() {
    this.setState({ count: this.state.count - 1 });
  }
  render() {
    return this.props.render({
      ...this.state,
      ...this.actions,
    });
  }
}

export default Generator(Counter);

它在初始设置为 0 或自定义值的状态中存储 count 值 由 initialCount 属性提供。 它有 increment()decrement() 方法 分别从计数中加或减一。

constructor() 方法中我们还定义了 this.actions 来存储这些的版本 绑定到 this 的方法,允许它们作为函数从任何地方调用 在我们的代码中。 我在这里给出了较短的名称 - incdec 但这完全是 随意的。 原来的名字也能达到目的。

到目前为止,这都是相当标准的 Javascript/React 代码。 有趣的地方是 render() 方法。 我们在这里所做的就是调用传递的 render() 函数 作为一个属性 - this.props.render() - 并向它传递一个包含这些东西的对象 我们想与其他组件共享。

在这种情况下,我们将传递状态中的所有内容(仅在这个简单示例中 包含 count) 和我们定义的操作状态的操作(incdec)。

最后一行代码调用从中导入的 Generator() 函数 @abw/react-context-generatorCounter 组件作为参数传递。 这就是 Counter.js 模块导出的默认值。

export default Generator(Counter);

Using the Context Provider

现在,当我们导入这个模块时,我们会得到一个包含两个条目的对象:一个 Provider 和一个 Consumer

Provider 旨在封装可能需要访问状态的所有组件。 它通常添加在应用程序“外部”附近的某个位置。

// MyApp.js
import React from "react";
import Counter from "./Counter";

export default props => <div id="myapp">
  <h1>Welcome to My Counting App!</h1>
  <Counter.Provider>
    ...the rest of your app goes here...
  </Counter.Provider>
</div>

如果需要,您可以将自定义属性传递给您的 Provider。 他们将被传递给 Counter 组件,例如,

export default props => <div id="myapp">
  <h1>Welcome to My Counting App that Starts at 100!</h1>
  <Counter.Provider initialCount={100}>
    ...the rest of your app goes here...
  </Counter.Provider>
</div>

Using the Context Consumer

当您需要从应用程序深处的某处访问 Counter 提供的状态时,您 只需要将它包装在 Counter.Consumer 函数中。 Counter 的所有项目 作为上下文的模块共享将作为属性传递到您的组件中。

// Controls.js
import React from "react";
import Counter from "./Counter";

const Controls = ({count, inc, dec}) => <div>
  <p>The current count is {count}</p>
  <button onClick={inc}>Increment</button>
  <button onClick={dec}>Decrement</button>
</div>

export default Counter.Consumer(Controls)

这里的所有都是它的。 甜蜜而简单!

react-context-generator

Introduction

This is a small and simple module that provides some syntactic sugar to streamline the use of React Context to maintain state in your React applications.

Seriously, it's embarassingly small. We're talking 20 lines of code or so.

If you're not already familiar with React Context then you should start by reading the React Context documentation: https://reactjs.org/docs/context.html

Installation

Add react-context-generator to your project using either npm or yarn

Either:

$ npm install --save @abw/react-context-generator

Or:

$ yarn add @abw/react-context-generator

Wrapping a Component that Maintains State

Here's a simple example of a component that maintains some state and provides methods for manipulating that state.

// Counter.js
import React from "react";
import Generator from "@abw/react-context-generator";

class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: this.props.initialCount || 0
    };
    this.actions = {
      inc: this.increment.bind(this),
      dec: this.decrement.bind(this),
    };
  }
  increment() {
    this.setState({ count: this.state.count + 1 });
  }
  decrement() {
    this.setState({ count: this.state.count - 1 });
  }
  render() {
    return this.props.render({
      ...this.state,
      ...this.actions,
    });
  }
}

export default Generator(Counter);

It stores a count value in the state which is initially set to 0 or a custom value provided by an initialCount property. It has increment() and decrement() methods which respectively add or subtract one from the count.

In the constructor() method we also define this.actions to store versions of these methods that are bound to this, allowing them to be called as functions from anywhere in our code. I've given then shorter names here - inc and dec but that's entirely arbitrary. The original names would serve the purpose just as well.

So far this is all fairly standard Javascript/React code. Where it gets interesting is in the render() method. All we do here is call the render() function that is passed in as a property - this.props.render() - and pass it an object containing the things that we want to share with other components.

In this case we're passing everything in the state (which in this simple example only contains count) and the actions that we've defined to manipulate the state (inc and dec).

The final line of code calls the Generator() function imported from @abw/react-context-generator passing the Counter component as an argument. This is then the default value exported by the Counter.js module.

export default Generator(Counter);

Using the Context Provider

Now when we import this module we get an object that contains two entries: a Provider and a Consumer.

The Provider is designed to enclose any and all components that might need to access the state. It's typically added somewhere near the "outside" of your application.

// MyApp.js
import React from "react";
import Counter from "./Counter";

export default props => <div id="myapp">
  <h1>Welcome to My Counting App!</h1>
  <Counter.Provider>
    ...the rest of your app goes here...
  </Counter.Provider>
</div>

You can pass custom properties to your Provider if you want to. They will get passed down to the Counter component, e.g.

export default props => <div id="myapp">
  <h1>Welcome to My Counting App that Starts at 100!</h1>
  <Counter.Provider initialCount={100}>
    ...the rest of your app goes here...
  </Counter.Provider>
</div>

Using the Context Consumer

When you need to access the state provided by Counter from somewhere deep inside your app you simply need to wrap it in the Counter.Consumer function. All of the items that the Counter module shares as context will be passed into your component as properties.

// Controls.js
import React from "react";
import Counter from "./Counter";

const Controls = ({count, inc, dec}) => <div>
  <p>The current count is {count}</p>
  <button onClick={inc}>Increment</button>
  <button onClick={dec}>Decrement</button>
</div>

export default Counter.Consumer(Controls)

That's all there is to it. Sweet and simple!

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文