- 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');
推薦閱讀
- WildFly:New Features
- R語言經(jīng)典實例(原書第2版)
- 新一代通用視頻編碼H.266/VVC:原理、標準與實現(xiàn)
- UI智能化與前端智能化:工程技術(shù)、實現(xiàn)方法與編程思想
- SQL基礎(chǔ)教程(視頻教學版)
- QGIS:Becoming a GIS Power User
- Python貝葉斯分析(第2版)
- 小學生C++創(chuàng)意編程(視頻教學版)
- 批調(diào)度與網(wǎng)絡(luò)問題的組合算法
- Visual Basic程序設(shè)計習題與上機實踐
- Advanced UFT 12 for Test Engineers Cookbook
- 深入解析Java編譯器:源碼剖析與實例詳解
- IPython Interactive Computing and Visualization Cookbook
- Maven for Eclipse
- Applied Deep Learning with Python