- Build Applications with Meteor
- Dobrin Ganev
- 243字
- 2021-07-09 19:48:54
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:
- 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
- Import createContainer into the Timer.js component:
import React from 'react';
import TimerDisplay from './TimerDisplay';
import { createContainer } from 'react-meteor-data';
- 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:
- We defined Time as a MongoDB collection.
- 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.
- 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');
推薦閱讀
- 流量的秘密:Google Analytics網(wǎng)站分析與優(yōu)化技巧(第2版)
- SQL Server 2016從入門(mén)到精通(視頻教學(xué)超值版)
- 小創(chuàng)客玩轉(zhuǎn)圖形化編程
- 編寫(xiě)整潔的Python代碼(第2版)
- Java程序設(shè)計(jì)與實(shí)踐教程(第2版)
- Xamarin.Forms Projects
- Spring+Spring MVC+MyBatis整合開(kāi)發(fā)實(shí)戰(zhàn)
- Visual C++開(kāi)發(fā)入行真功夫
- 軟件測(cè)試實(shí)用教程
- 區(qū)塊鏈技術(shù)進(jìn)階與實(shí)戰(zhàn)(第2版)
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計(jì)原理(第2版)
- UI設(shè)計(jì)基礎(chǔ)培訓(xùn)教程(全彩版)
- Learning Concurrency in Python
- iOS Development with Xamarin Cookbook
- Python實(shí)戰(zhàn)指南:手把手教你掌握300個(gè)精彩案例