Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

vaadin/base-starter-react

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Base Starter for Vaadin components with React

Prerequisites

First install yarn.

Running the application

You can run the application by issuing the following commands at the root of the project in your terminal window:

$ yarn install
$ yarn start

This project was bootstrapped with Create React App.

Find information on how to perform common tasks in this guide.

Recreating this project from scratch

Initialize using Create React App

$ yarn global add create-react-app

$ create-react-app hello-react
$ cd hello-react

$ yarn install

Update browsers list

Vaadin components support modern browsers and IE11, so the browserslist section in package.json should be updated to look like this:

  "browserslist": [
    "last 2 versions",
    "not dead",
    "ie 11",
    "not op_mini all"
  ]

Install dependencies

$ yarn add @vaadin/vaadin-core
$ yarn add @webcomponents/webcomponentsjs

Add Web Components polyfill

  1. Install the utility to copy polyfills:
$ yarn add vendor-copy
  1. Update scripts section in package.json and add the line:
  "postinstall": "vendor-copy"
  1. Add the following section to package.json:
  "vendorCopy": [
    {
      "from": "node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js",
      "to": "public/vendor/custom-elements-es5-adapter.js"
    },
    {
      "from": "node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js",
      "to": "public/vendor/webcomponents-bundle.js"
    }
  ],
  1. Open public/index.html and add the following lines in the <head> section:
  <script src="%PUBLIC_URL%/vendor/webcomponents-bundle.js"></script>
  <script>if (!window.customElements) { document.write('<!--'); }</script>
  <script src="%PUBLIC_URL%/vendor/custom-elements-es5-adapter.js"></script>
  <!--! DO NOT REMOVE THIS COMMENT, WE NEED ITS CLOSING MARKER -->
  1. Make sure the scripts are copied:
$ yarn postinstall

Add Web Components to the project

Open src/App.js and do the following modifications.

  1. Import Vaadin components:
import '@vaadin/vaadin-button/vaadin-button.js';
import '@vaadin/vaadin-text-field/vaadin-text-field.js';
  1. Define a constructor for the App component:
constructor(props) {
  super(props);
  this.state = {greeting: "React App"};
  this.clicked = this.clicked.bind(this);
  this.textField = React.createRef();
}
  1. Update the render method of the App component to return HTML:
<div className="App">
  <vaadin-text-field ref={this.textField} placeholder="Type Something"></vaadin-text-field>
  <vaadin-button onClick={this.clicked}>Click Me!</vaadin-button>
  <h2>Hello {this.state.greeting}!</h2>
</div>
  1. Define the click event handler:
clicked() {
  this.setState({greeting: this.textField.current.value})
}