This article mainly introduces how to migrate your AngularJS1.x application to React. Now I will share it with you and give you a reference.
Angular and React are both great frameworks/libraries. Angular provides the definition structure of MVC (Model, View, Controller). React provides a lightweight rendering mechanism based on state changes. Often, after developers have an old application on AngularJS, they want to build new features using ReactJS.
While removing the AngularJS application, building a ReactJS application from scratch is a good option. But for large-scale applications, it is not a feasible solution. In this case, it would be easier to build a separate React component and import it into Augular.
In this article, I will help you create a React component in an Angular application using react2angular.
Goals and Plans
Here is what we are going to do -
Given: A page to display the name of the city and its famous attractions Angular application.
Goal: Add a React component to the Angular application. The React component will display a featured photo of the attraction.
Plan: We will create a React component, pass the image URL, and display the image as a React component.
let's start!
Step 0: Have an Angular App
For the purposes of this article, keep your Angular app simple. I'm planning a trip to Europe in 2018, so my Angular app is essentially a list of destinations I want to visit.
Here's what the dataset bucketlist looks like:
const bucketlist = [{ city: 'Venice', position: 3, sites: ['Grand Canal', 'Bridge of Sighs', 'Piazza San Marco'], img: 'https://unsplash.com/photos/ryC3SVUeRgY', }, { city: 'Paris', position: 2, sites: ['Eiffel Tower', 'The Louvre', 'Notre-Dame de Paris'], img: 'https://unsplash.com/photos/R5scocnOOdM', }, { city: 'Santorini', position: 1, sites: ['Imerovigli', 'Akrotiri', 'Santorini Arts Factory'], img: 'https://unsplash.com/photos/hmXtDtmM5r0', }];
This is what angularComponent.js looks like:
function AngularComponentCtrl() { var ctrl = this; ctrl.bucketlist = bucketlist; }; angular.module('demoApp').component('angularComponent', { templateUrl: 'angularComponent.html', controller: AngularComponentCtrl });
This is angularComponent.html:
<p ng-repeat="item in $ctrl.bucketlist" ng-sort="item.position"> <h2>{{item.city}}</h2> <p> I want to see <span ng-repeat="sight in item.sights">{{sight}} </p></span> </p>
supersimple! Now it's time to go to Santorini...
Step 1: Install dependencies
If your editor is not configured, then migrate from Angular to React It can be painful. We will install linting first.
npm install --save eslint babel-eslint
Next, install react2angular. If you have never installed React, you will also need to install react, react-dom and prop-types.
npm install --save react2angular react react-dom prop-types
Step 2: Create a React component
Now, we have an Angular component to render the name of the city. Next, we need to render the featured image. Let's say this image is provided to us via props. Our React component looks like this:
import {Component} from 'react'; class RenderImage extends Component { render() { const imageUrl = this.props.imageUrl; return ( <p> <img src={imageUrl} alt=""/> </p> ); } }
Step 3: Pass props attribute
Remember that in step 2, assume there is a props obtained through available images. We now want to fill in the props value. You can pass dependencies to React components using props. It is important to note that the React component does not have any Angular dependencies available. You can think of it this way - a React component is like a container connected to an Angular application. If you need the container to inherit information from its parent, you will need to access it explicitly via props.
So, in order to pass dependencies, we will add a renderImage component in Angular and pass it as a parameter to imageUrl:
angular.module('demoApp', []) .component('renderImage', react2angular(RenderImage,['imageUrl']));
Step 4: Import Angular Template
Now you can simply import this component into your Angular app like any other component:
<p ng-repeat="item in $ctrl.bucketlist"> <h2>{{item.city}}</h2> <p> I want to see <span ng-repeat="site in item.sites">{{site}}</span> <render-image image-url={{item.img}}></render-image> </p>
Ta Da! I can't believe it, this is magic. Of course, it's (more) hard work and sweat, coffee to accompany us, etc.
Let’s build some React components now, warrior!
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to get the first value in the select drop-down box in JavaScript
How to do it in AngularJS Get and display passwords in real time
How to implement offset and uniform animation in JS
The above is the detailed content of Migrate AngularJS1.x applications to React (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!