npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

icp-react-in-angularjs

v18.0.0

Published

A super simple way to render React components in AngularJS

Downloads

5

Readme

react-in-angularjs

A super simple way to render React components in AngularJS. This was written with the goal of facilitating conversion of an AngularJS app without doing a full rewrite.

Install

The function contained in this library is small enough that you might just want to copy paste it. Feel free to do so. Otherwise:

npm install react-in-angularjs

yarn add react-in-angularjs

Usage

import React from "react";
import {angularize} from "react-in-angularjs";

// Note: This also works with class components
const TodoList = ({todos}) =>  {
  return (
    <ol>
      {todos.map(todo => (
        <li key={todo._id}>
          {todo.description}
        </li>
      ))}
    </ol>
  );
};

angularize(TodoList, "todoList", angular.module("app"), {
  todos: "<"	
});

// You don't actually have to export anything to make this work but
// you'll likely want to for tests and use in other components
export default TodoList;

TodoList React component now wrapped in an AngularJS component named "todoList". Sometime later in your app...

<todo-list todos="todos"></todo-list>

Building

Since you likely don't have a normal React entry point, you'll need to leverage webpack's ability to have multiple entry points. I accomplish this in my own project using glob:

const glob = require("glob");
const path = require("path");

module.exports = {
  devtool: "source-map",
  entry: glob.sync("./src/**/!(*.test).jsx"),
  output: {
    filename: "[name].js",
    path: path.resolve(__dirname, "dist") 
  }
};

Services

When you need to access an Angular service, you can access it with a separate function:

// AngularJS code includes a service you'd like to use and can't rewrite yet:
window.angular.module("myApp").service("todoService", () => {
  // Some very lovingly crafted service code
});

import {getService} from "react-in-angularjs"
const todoService = getService("todoService");
// Now you've got the singleton instance of it

This can also be used to fetch built-in AngularJS services like $timeout, $http, etc.

Directives

Sometimes you really need the resulting component to be a directive, typically when doing tables. For those situations, do this:

import React from "react";
import {angularizeDirective} from "react-in-angularjs";

const SpecialTableHeader = ({data}) =>  {
  const sort = () => {
    // Very special sort logic
  } 

  return (  
    <thead>
      <tr>
        <th onClick={sort}>Something</th>
      </tr>
    </thead>
  );
};

angularizeDirective(SpecialTableHeader, "specialTableHeader", angular.module("app"), {
  data: "<"	
});
<table>
    <thead special-table-header data="data"></thead>
    <tbody>
    ...etc.
</table>

By default this uses replace: true so your HTML stays intact with no wrapping tag.

Caveats

AngularJS within React

You can't use AngularJS components within the React render so you'll need to work bottom up, i.e. replace low level components first. Low level components can be then be imported directly into your React components as well as used in legacy AngularJS (assuming they are angularized).

Two Way Bindings

Two way bindings are not recommended, either by the AngularJS team or by me. However, it's not always possible to remove them in a legacy application. In those cases, you can apply changes in two ways:

Use $timeout
const TodoItem = ({todo}) => {
  // imagine some React component with a change handler
  const onChange = () => {
    // get Angular's $timeout wrapper using getService
    const $timeout = getService("$timeout"); 
    $timeout(() => {
      todo.value = "new value"
    });
  }	
}
Use $scope

react-in-angularjs provides the wrapping AngularJS's component $scope as a prop

const TodoItem = ({todo, $scope}) => {
  // imagine some React component with a change handler
  const onChange = () => {
    $scope.$apply(() => {
      // $scope = AngularJS component scope, provided on a prop
      todo.value = "new value"
    });
  };
}