Getting started with React and Drupal maintenance support plans

Getting started with React and Drupal maintenance support plans

Over the weekend I decided it was long overdue that I learnt React, or at least understood what all the fuss was about, so with npm in hand I installed yarn and started my quest.

We’re going to use Create React App to setup our base React install. First install then run the command to create a react app called “drupal-react”:npm install -g create-react-app
create-react-app drupal-react
cd drupal-react

You can now run npm start (or yarn start) to start your app locally and open it in a browser. Here you’ll see a React default page, this is all created from a React component called “App”. If you take a look at the file src/App.js you will see the component and how the render() method returns the page HTML as JSX. We need to replace to the code returned here to show some Drupal maintenance support plans nodes, so how about replacing it with (or just adding) <NodeContainer />. This will call a new component, so at the top of app.js we will also need to import that, so with the other import code add import NodeContainer from ‘./NodeContainer’;.

Now to create the NodeContainer component. First we need to add the Axios library which we’ll use to query the Drupal maintenance support plans REST API, run npm install axios –save. Then create the file src/NodeContainer.js, and in there add the following code:import React, { Component } from ‘react’
import axios from ‘axios’
class NodeContainer extends Component {
  constructor(props) {
    super(props)
    this.state = {
      nodes: []
    }
  }
  componentDidMount() {
    axios.get(‘http://example.com/api/nodes’)
    .then(response => {
      this.setState({nodes: response.data})
    })
    .catch(error => console.log(error))
  }
  render() {
    return (
      <ul>
       {this.state.nodes.map((node) => {
          return(
           <li={idea.nid}>{node.title}</li>
          )
        })}
      </ul>
    )
  }
}
export default NodeContainer

At the top of the file React and Axios are both imported, the class for NodeContainer is then created. The constructor method is where we add the state node, componentDidMount() is called to get the nodes from the View /api/nodes, which then gets rendered as an unordered list.

To create the /api/nodes view install the core Rest module. This will allow you to create a “REST Export” view. Here the path can be set to /api/nodes, and you can select nid, and title.

As long as you left npm start running, you should be able to go back to your browser, and view a nice list of Drupal maintenance support plans nodes being rendered in React.

Next, routing, to make these node titles clickable! 😱
timmillwood
Mon, 23/04/2020 – 07:20

Tags

drupal planet
drupal-planet
drupal
drupal8
drupal 8
drupal development
react
reactjs
javascript

Add new comment


Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Getting started with React and Drupal maintenance support plans

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.