官术网_书友最值得收藏!

Integrating React with Meteor's reactive data system

In the preceding example, the data passed to the child component via props was generated in the parent component itself in the setInterval function. In order to render React components on Meteor's data change, we need to create a component container.

The steps are as follows:

  1. Add react-meteor-data and react-addons-pure-render-mixin npm packages using the following command:
        >> npm install react-meteor-data --save
>> npm install react-addons-pure-render-mixin --save
  1. Import createContainer into the Timer.js component:
        import React from 'react';
import TimerDisplay from './TimerDisplay';
import { createContainer } from 'react-meteor-data';
  1. Export the container function instead of the component:
        export default createContainer(() => {
return {
time: Time.find().fetch()
};
},
Timer);

The first parameter is a callback function that returns the result as an object named time, and the second parameter is the Timer component. The way it works is that on any changes in the browser database Minimongo, the data will be fetched and passed to the component as props (time in our case).

To break this down, refer to this:

  1. We defined Time as a MongoDB collection.
  2. find() is a MongoDB method to query records (records are called documents in MongoDB) from the collection. If there is no query specified, will return cursor for the first 20 records by default.
  3. Adding the fetch() method will return the documents as an array.

Meteor allows us to create a collection directly in the code:

Time = new Mongo.Collection('time');
主站蜘蛛池模板: 厦门市| 麻栗坡县| 吉林市| 清涧县| 衡南县| 荣成市| 新和县| 昌都县| 仙桃市| 二连浩特市| 肇东市| 城口县| 即墨市| 于田县| 东莞市| 尼勒克县| 龙胜| 齐齐哈尔市| 景泰县| 新津县| 靖州| 寻乌县| 淅川县| 遵义市| 海兴县| 开化县| 广安市| 游戏| 喀喇沁旗| 肃宁县| 泽普县| 建宁县| 灵丘县| 黑山县| 安乡县| 章丘市| 伊春市| 扎赉特旗| 霍城县| 青海省| 九龙县|