@bitrise/react2angular

中文文档
5.0.0发布于 2022/07/19Apache-2.0TypeScriptCommonJS近 30 天下载 4198

The easiest way to embed React components in Angular 1 apps!

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. 1. Create a React component
  4. 2. Expose it to Angular
  5. 3. Use it in your Angular 1 code
  6. Dependency Injection
  7. Tests
  8. License
React to Angular: The easiest way to use React components in Angular 1

react2angular Build Status NPM Apache2

The easiest way to embed React components in Angular 1 apps! (opposite of angular2react)

Installation

# Using Yarn:
yarn add react2angular react react-dom prop-types

# Or, using NPM:
npm install react2angular react react-dom prop-types --save

Usage

1. Create a React component

import { Component } from 'react'

class MyComponent extends Component {
  render() {
    return <div>
      <p>FooBar: {this.props.fooBar}</p>
      <p>Baz: {this.props.baz}</p>
    </div>
  }
}

2. Expose it to Angular

import { react2angular } from 'react2angular'

angular
  .module('myModule', [])
  .component('myComponent', react2angular(MyComponent, ['fooBar', 'baz']))

Note: If you defined propTypes on your component, they will be used to compute component's bindings, and you can omit the 2nd argument:

...
  .component('myComponent', react2angular(MyComponent))

If propTypes are defined and you passed in a 2nd argument, the argument will override propTypes.

3. Use it in your Angular 1 code

<my-component
  foo-bar="3"
  baz="'baz'"
></my-component>

Note: All React props are converted to AngularJS one-way bindings. If you are passing functions into your React component, they need to be passed as a function ref, rather than as an invokable expression. Keeping an existing AngularJS-style expression will result in infinite loops as the function re-evaluates on each digest loop.

Dependency Injection

It's easy to pass services/constants/etc. to your React component: just pass them in as the 3rd argument, and they will be available in your component's Props. For example:

import { Component } from 'react'
import { react2angular } from 'react2angular'

class MyComponent extends Component {
  state = {
    data: ''
  }
  componentDidMount() {
    this.props.$http.get('/path').then(res =>
      this.setState({ data: res.data })
    )
  }
  render() {
    return <div>
      { this.props.FOO }
      { this.state.data }
    </div>
  }
}

angular
  .module('myModule', [])
  .constant('FOO', 'FOO!')
  .component('myComponent', react2angular(MyComponent, [], ['$http', 'FOO']))

Note: If you have an injection that matches the name of a prop, then the value will be resolved with the injection, not the prop.

Tests

npm test

License

Apache2

版本与发布记录

@bitrise/react2angular 共发布 1 个版本,首次发布于 2022/07/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.0latest2022/07/194 年前

关于 @bitrise/react2angular 的常见问题

@bitrise/react2angular 是什么?

The easiest way to embed React components in Angular 1 apps!

如何安装 @bitrise/react2angular?

在项目目录运行 npm install @bitrise/react2angular,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)、react-dom(>=18)、@types/react(>=18)、@types/angular(>=1.5)、@types/react-dom(>=18)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bitrise/react2angular 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bitrise/react2angular 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bitrise/react2angular 可以商用吗?

@bitrise/react2angular 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bitrise/react2angular 的下载量有多少?

根据 npm 官方下载统计,@bitrise/react2angular 在 2026/10/08 – 2026/11/06 这 30 天内被下载 4,198 次,日均约 140 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bitrise/react2angular 还在维护吗?

最新版本 5.0.0 发布于 2022/07/19(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/07/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。