React、Meteor 用户的福音,使用 Meteor 构建先进的 Web 项目

2017-03-03 12:28:24 +08:00
 zevenfang

react-meteor

Meteor Reactivity for your React application :).

What is it for ?

The purpose of this library is :

Install

npm i --save react-web-meteor

Example usage


import React, { Component } from 'react';
import Meteor, { createContainer } from 'react-web-meteor';

Meteor.connect('ws://192.168.X.X:3000/websocket');//do this only once

class Todo extends Component {
  renderRow(todo) {
    return (
      <span>{todo.title}</span>
    );
  }
  render() {
    const { settings, todosReady, todos } = this.props;
    return(
      <div>
        <div>{settings.title}</div>
        {!todosReady && <span>Not ready</span>}
        <div>{todos.map(v=>this.renderRow(v))}</div>
      </div>
    )
  }
}

export default createContainer(params=>{
  const handle = Meteor.subscribe('todos');
  Meteor.subscribe('settings');
  return {
    todosReady: handle.ready(),
    settings: Meteor.collection('settings').findOne()
  };
}, Todo)

Connect your components

Since Meteor 1.3, createContainer is the recommended way to populate your React Components.

createContainer

Very similar to getMeteorData but your separate container components from presentational components.

Example

import Meteor, { createContainer } from 'react-web-meteor';


class Orders extends Component {
  render() {
    const { pendingOrders } = this.props;

    //...
    );
  }
}

export default createContainer(params=>{
  return {
    pendingOrders: Meteor.collection('orders').find({status: "pending"}),
  };
}, Orders)

Reactive variables

These variables can be used inside getMeteorData or createContainer. They will be populated into your component if they change.

Additionals collection methods

These methods (except update) work offline. That means that elements are correctly updated offline, and when you reconnect to ddp, Meteor calls are taken care of.

API

Meteor Collections

Meteor.subscribe

Meteor.subscribe() returns an handle. If the component which called subscribe is unmounted, the subscription is automatically canceled.

Meteor.collection(collectionName, options)

You need pass the cursoredFind option when you get your collection if you want to use cursor-like method:

Meteor.collection("collectionName", { cursoredFind: true })

Or you can simply use find() to get an array of documents. The option default to false for backward compatibility. Cursor methods are available to share code more easily between a react app and a standard Meteor app.

Meteor DDP connection

Meteor.connect(endpoint, options)

Connect to a DDP server. You only have to do this once in your app.

Arguments

Meteor.disconnect()

Disconnect from the DDP server.

Meteor methods

Availables packages

Convenience packages

Example `import { composeWithTracker } from 'react-web-meteor';``

ReactiveDict

See documentation.

Meteor.Accounts

`import { Accounts } from 'react-web-meteor';``

Meteor.ddp

Once connected to the ddp server, you can access every method available in ddp.js.

Author

Want to help ?

Pull Requests and issues reported are welcome! :)

2044 次点击
所在节点    前端开发
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/344593

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX