Open In App

Meteor Tracker

Last Updated : 31 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Meteor is a full-stack JavaScript platform that is used for developing modern web and mobile applications. Meteor has a set of features that are helpful in creating a responsive and reactive web or mobile application using JavaScript or different packages available in the framework. It is used to build connected-client reactive applications.

Tracker is a package that allows templates and other calculations to be quickly replicated when variables, database queries, or other data sources are changed. Tracker.autorun lets us run a function that relies on reactive data sources in such a way that the function is replayed if the data changes later.

Syntax:

Tracker.autorun(function () {
    ...
});

 

Creating Meteor Application And Importing Module:

Step 1: Create a React application using the following command.

meteor create foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command.

cd foldername

Project Structure: It will look like the following.

Step to Run Application: Run the application from the root directory of the project, using the following command.

meteor

Example: This is the basic example that shows how to use the Tracker component.

Main.html




<head>
    <title>GeeksforGeeks</title>
</head>
  
<body>
    <div>
        {{> table}}
    </div>
</body>
  
<template name="table">
    <h1 class="heading">GeeksforGeeks</h1>
    <p>
        Click on the button below to get the
        next value of the table of 12.
    </p>
  
    <button id="myButton">NEXT</button>
</template>


Main.js




import './main.html';
  
let count = 0;
Session.set('value', count);
  
Tracker.autorun(function () {
    let result = Session.get('value');
    console.log(`12 x ${result} = ${result * 12}`);
});
  
Template.table.events({
  
    'click #myButton': function () {
        Session.set('value', count++);
    }
});


Output:

Reference: https://docs.meteor.com/api/tracker.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads