Code Monkey home page Code Monkey logo

base-starter-react's Introduction

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})
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.