Onsen UI QUICK START FOR REACT

To use Onsen UI in React apps, Onsen UI Core and React Components should be installed to the project. These modules can be installed via NPM package: onsenui and react-onsenui.

To quickly setup the project, Monaca CLI will solve all dependencies including TypeScript, Webpack and polyfills if necessary.

Using Onsen UI toolkit - Monaca CLI

$ npm install -g monaca # Install Monaca CLI - Onsen UI toolkit
$ monaca create helloworld # Choose React template
$ cd helloworld; monaca preview # Run preview, or "monaca debug" to run on your device

Download via npm

# The "react-onsenui" library requires the "onsenui" library.
$ npm install --save-dev onsenui react-onsenui

Use Kitchensink Code

Onsen UI React Components Kitchensink contains almost all components in Onsen UI as one application package. Link to Kitchensink Demo.

The code in this example is transpiled using Babel and bundled with Browserify.

Loading Onsen UI for React

Onsen UI for React is an extension to Onsen UI core, a Web components based UI framework. You need to load the following two JavaScript modules.

  • Onsen UI Core (GitHub)
  • Onsen UI for React Extension (GitHub)

You can load with a normal  tag has text/babel type. This means this script is not a pure JavaScript that browser supports (most commonly ECMAScript5), but is a ECMAScript6 (ES2015) with JSX format. Babel will transpile this code into ES5 in the browser. To get better performance, we can use node.js to transpile the code.

Inside the script, React.createClass() function defines a React Component called App. It has one method called render, and this is the function that is called when the app is rendered. The returning object is described in JSX, which is a XML like language that extends JavaScript. In this case, it is returning  component that has an  component. The onClick prop is used to call the handleClick method when the user taps the button.

return (
  
    Tap me!
  
);

As you can see in this example, all  components are React Components, and they are loaded by react-onsenui.js. Click here to see our full Onsen UI React Components Reference.

Putting together, when index.html is loaded into the browser, it will compile JSX code, inject into the body, and render the content.

This is a basic introduction of how Onsen UI and React works together. If you want to further learn React, we recommend to read the official React docs.

Creating a page

The root of a page is created using the  element. It covers the whole screen and is used as a container for the other elements.

Adding a toolbar

A toolbar is defined as a  or  component. Here is the typical example of a toolbar.

<Page renderToolbar={() =>
  <Toolbar>
    <div className="left">
      <BackButton>BackBackButton>
    div>
    <div className="center">Titlediv>
    <div className="right">
      <ToolbarButton>
        <Icon icon="md-menu" />
      ToolbarButton>
    div>
  Toolbar> }
>
  Static page app
Page>

The toolbar is divided into 3 sections that can be specified as class names (leftcenter, and right). You can use  to display an icon,  or  to place an button, or insert any HTML content.

Event Handling

Onsen UI components are capable of handling events. For instance, you can catch a tap event by using the onClickprop, or text input change with the onChange prop.

class MyPage extends React.Component {
  handleClick() {
    ons.notification.alert('Hello, world!');
  }
  render() {
    return() (
      <Page>
         <Button onClick={this.handleClick}>Click me!Button>
      Page>
    );
  }
}

The ons object

Onsen UI not only provides custom elements, it also exposes an object called ons with a lot of useful functions attached to it. The ons object is part of the core library and can be imported in the bindings.

The following example uses ons.ready(fn) which waits until the app is completely loaded before executing a callback function. Inside the callback, it is calling ons.notification.alert() to display a alert dialog.

ons.ready(function() {
  // Onsen UI is now initialized
  ons.notification.alert('Welcome to Onsen UI!');
});

See also ons.platformons.notification and ons.orientation for more utilities.

Importing ons object in React

Simply use ES6 imports:

import ons from `onsenui`;
import { platfrom, notification } from `onsenui`;

Adding page content

For a full list of components please check the reference page.

Form elements

Onsen UI provides a rich set of form components. Apart from 

你可能感兴趣的:(ReactJS,React开发教程笔记)