Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5ef5d32
feat: Add example and Meteor v.3 support, add types
kolyasya Dec 7, 2023
60e2648
feat: Move code to ./src before publish
kolyasya Dec 7, 2023
0a3bad4
feat: Update code, add logging and params, update docs
kolyasya Dec 9, 2023
7657b0c
feat: Adjust pre-publish
kolyasya Dec 9, 2023
3e57d4a
fix: Update gitignore
kolyasya Jan 19, 2024
7010cf8
fix: Update docs
kolyasya Jan 19, 2024
6b4a0a2
feat: Add ability to override params during call
kolyasya Jan 19, 2024
5e8482b
feat: Add ability to override params call, adjust types
kolyasya Jan 19, 2024
d221b2e
fix: Adjust package version
kolyasya Jan 19, 2024
215f845
feat: Add ability to validate params
eadenink May 28, 2024
dc8dd41
feat: Update params passing
eadenink May 28, 2024
1e746b3
feat: Update package in example
eadenink May 28, 2024
390a84e
fix: Default validate function
eadenink May 28, 2024
8627183
feat: Update example for the new params passing
eadenink May 28, 2024
07f3319
feat: Update Meteor
kolyasya Jun 5, 2024
16eb9b1
feat: Implement separate function to transform params
eadenink Jun 6, 2024
5a0bd9e
fix: Adjust validation error message
kolyasya Jun 6, 2024
9af5999
Merge pull request #1 from kolyasya/feature/add-ability-to-validate-p…
kolyasya Jun 6, 2024
6279b91
fix: Version Up
kolyasya Jun 6, 2024
4a59459
feat: Prepare new version
kolyasya Jun 6, 2024
5b77fb8
feat: Add transform example to readme
eadenink Jun 6, 2024
50efc13
feat: Fix types approach
kolyasya Aug 5, 2024
45d7d27
feat: Update types approach again
kolyasya Aug 5, 2024
b3344c8
fix: Optimize state initialization in useMeteorCall
Bboyredstar Sep 8, 2025
5c03c51
Merge pull request #2 from Bboyredstar/patch-1
kolyasya Sep 8, 2025
40958d4
fix: Make sure we run callback in case of error
kolyasya Nov 21, 2025
36ecec6
fix: Make sure we stop loading on error
kolyasya Nov 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
54 changes: 41 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# react-meteor-method-hooks

Simple hook to handle `Meteor.call` requests.
Updated to work with `Meteor.callAsync()` method.

Simple hook to handle `Meteor.callAsync()` or `Meteor.call()` requests.

Usage example:

Expand All @@ -11,25 +13,51 @@ import { useMeteorCall } from 'react-meteor-method-hooks';
const MyComponent = () => {

const [
calculateSomething,
calculateSomethingLoading,
calculateSomethingError,
calculateSomething,
calculateSomethingLoading,
calculateSomethingError,
calculateSomethingResult
] = useMeteorCall(
'calculateSomethingMethodName',
{},
(error, result) => {
if (error) {
alert(error.reason);
} else {
console.log(result);
}
{
/** Callback to be executed after the method is called */
cb: (error, result) => {
if (error) {
alert(error.reason);
} else {
console.log(result);
}
},

/** Validate params before handler execution */
validate: (...params) => {
const param = params[0];

return param.methodParam === 'Test string';
},
/** Transform params after validation before handler execution */
transform: (...params) => {
const param = params[0];

return [{ ...param, methodParam: 'Transformed string' }];
},

/** Forces to use Meteor.call() instead of Meteor.callAsync() */
forceSyncCall: true,
/** Adds some logging in console */
logging: true,
/**
* By default the package logs console.error for all incoming errors
* The setting disables such logs
*/
suppressErrorLogging: true
},
{ methodParam: 'Test string' },
);

return (
<button
onClick={calculateSomething}
<button
onClick={calculateSomething}
disabled={calculateSomethingLoading}
>
Calculate
Expand Down
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
19 changes: 19 additions & 0 deletions example/.meteor/.finished-upgraders
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file contains information which helps Meteor properly upgrade your
# app when you run 'meteor update'. You should check it into version control
# with your project.

notices-for-0.9.0
notices-for-0.9.1
0.9.4-platform-file
notices-for-facebook-graph-api-2
1.2.0-standard-minifiers-package
1.2.0-meteor-platform-split
1.2.0-cordova-changes
1.2.0-breaking-changes
1.3.0-split-minifiers-package
1.4.0-remove-old-dev-bundle-link
1.4.1-add-shell-server-package
1.4.3-split-account-service-packages
1.5-add-dynamic-import-package
1.7-split-underscore-from-meteor-base
1.8.3-split-jquery-from-blaze
1 change: 1 addition & 0 deletions example/.meteor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local
7 changes: 7 additions & 0 deletions example/.meteor/.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file contains a token that is unique to your project.
# Check it into your repository along with the rest of this directory.
# It can be used for purposes such as:
# - ensuring you don't accidentally deploy one app on top of another
# - providing package authors with aggregated statistics

mwteyo9y63m.nsvo9ukypcz7
23 changes: 23 additions & 0 deletions example/.meteor/packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Meteor packages used by this project, one per line.
# Check this file (and the other files in this directory) into your repository.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.

meteor-base@1.5.1 # Packages every Meteor app needs to have
mobile-experience@1.1.1 # Packages for a great mobile UX
mongo@1.16.10 # The database Meteor supports right now
reactive-var@1.0.12 # Reactive variable for tracker

standard-minifier-css@1.9.2 # CSS minifier run for production mode
standard-minifier-js@2.8.1 # JS minifier run for production mode
es5-shim@4.8.0 # ECMAScript 5 compatibility for older browsers
ecmascript@0.16.8 # Enable ECMAScript2015+ syntax in app code
typescript@4.9.5 # Enable TypeScript syntax in .ts and .tsx modules
shell-server@0.5.0 # Server-side component of the `meteor shell` command
hot-module-replacement@0.5.3 # Update client in development without reloading the page


static-html@1.3.2 # Define static page content in .html files
react-meteor-data # React higher-order component for reactively tracking Meteor data
zodern:types # Pull in type declarations from other Meteor packages
2 changes: 2 additions & 0 deletions example/.meteor/platforms
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server
browser
1 change: 1 addition & 0 deletions example/.meteor/release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
METEOR@2.16
72 changes: 72 additions & 0 deletions example/.meteor/versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
allow-deny@1.1.1
autoupdate@1.8.0
babel-compiler@7.10.5
babel-runtime@1.5.1
base64@1.0.12
binary-heap@1.0.11
blaze-tools@1.1.3
boilerplate-generator@1.7.2
caching-compiler@1.2.2
caching-html-compiler@1.2.1
callback-hook@1.5.1
check@1.4.1
ddp@1.4.1
ddp-client@2.6.2
ddp-common@1.4.1
ddp-server@2.7.1
diff-sequence@1.1.2
dynamic-import@0.7.3
ecmascript@0.16.8
ecmascript-runtime@0.8.1
ecmascript-runtime-client@0.12.1
ecmascript-runtime-server@0.11.0
ejson@1.1.3
es5-shim@4.8.0
fetch@0.1.4
geojson-utils@1.0.11
hot-code-push@1.0.4
hot-module-replacement@0.5.3
html-tools@1.1.3
htmljs@1.1.1
id-map@1.1.1
inter-process-messaging@0.1.1
launch-screen@2.0.0
logging@1.3.4
meteor@1.11.5
meteor-base@1.5.1
minifier-css@1.6.4
minifier-js@2.8.0
minimongo@1.9.4
mobile-experience@1.1.1
mobile-status-bar@1.1.0
modern-browsers@0.1.10
modules@0.20.0
modules-runtime@0.13.1
modules-runtime-hot@0.14.2
mongo@1.16.10
mongo-decimal@0.1.3
mongo-dev-server@1.1.0
mongo-id@1.0.8
npm-mongo@4.17.2
ordered-dict@1.1.0
promise@0.12.2
random@1.2.1
react-fast-refresh@0.2.8
react-meteor-data@2.7.2
reactive-var@1.0.12
reload@1.3.1
retry@1.1.0
routepolicy@1.1.1
shell-server@0.5.0
socket-stream-client@0.5.2
spacebars-compiler@1.3.1
standard-minifier-css@1.9.2
standard-minifier-js@2.8.1
static-html@1.3.2
templating-tools@1.2.2
tracker@1.3.3
typescript@4.9.5
underscore@1.6.1
webapp@1.13.8
webapp-hashing@1.1.1
zodern:types@1.0.13
4 changes: 4 additions & 0 deletions example/client/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
padding: 10px;
font-family: sans-serif;
}
8 changes: 8 additions & 0 deletions example/client/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<head>
<title>example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<div id="react-target"></div>
</body>
10 changes: 10 additions & 0 deletions example/client/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import { Meteor } from 'meteor/meteor';
import { App } from '/imports/ui/App';

Meteor.startup(() => {
const container = document.getElementById('react-target');
const root = createRoot(container!);
root.render(<App />);
});
10 changes: 10 additions & 0 deletions example/imports/api/links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Mongo } from 'meteor/mongo';

export interface Link {
_id?: string;
title: string;
url: string;
createdAt: Date;
}

export const LinksCollection = new Mongo.Collection<Link>('links');
109 changes: 109 additions & 0 deletions example/imports/ui/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React, { useEffect } from 'react';

import { useMeteorCall } from './react-meteor-method-hooks/useMeteorCall';

export const App = () => {
const [
methodWithResult,
methodWithResultLoading,
methodWithResultError,
methodWithResultResult,
] = useMeteorCall(
'methodWithResult',
{
cb: (error, result) => {
console.log('methodWithResult callback', { error, result });
},
validate: (...params) =>
params.every((param) => typeof param === 'string'),
transform: (...params) => params.map((p, i) => `${i + 1}-${p}`),
forceSyncCall: false,
logging: false,
suppressErrorLogging: false,
},
'one',
'two',
'three'
);

const [
methodWithError,
methodWithErrorLoading,
methodWithErrorError,
methodWithErrorResult,
] = useMeteorCall('methodWithError', {
cb: (error, result) => {
console.log('methodWithError callback', { error, result });
},
forceSyncCall: false,
logging: true,
suppressErrorLogging: false,
});

useEffect(() => {
methodWithResult();
methodWithError();
}, []);

useEffect(() => {
const timeout = setTimeout(() => {
// methodWithResult('one', 'two', 'three');
// methodWithResult();
// methodWithResult({});
Comment on lines +50 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This file contains commented-out code. If this code is no longer needed, it should be removed to improve code clarity and maintainability. If it's for demonstration, it would be better to explain its purpose in a comment rather than leaving dangling code.

methodWithResult({ customParam: true });
}, 15000);

return () => clearTimeout(timeout);
}, []);

useEffect(() => {
console.log({
methodWithResultLoading,
methodWithResultError,
methodWithResultResult,
methodWithErrorLoading,
methodWithErrorError,
methodWithErrorResult,
});
}, [
methodWithResultLoading,
methodWithResultError,
methodWithResultResult,
methodWithErrorLoading,
methodWithErrorError,
methodWithErrorResult,
]);

return (
<div style={{ display: 'flex' }}>
<div>
<h4>methodWithResult</h4>
<pre>
{JSON.stringify(
{
methodWithResultLoading,
methodWithResultError,
methodWithResultResult,
},
undefined,
2
)}
</pre>
</div>
<div>
<h4>methodWithError</h4>
<pre>
{JSON.stringify(
{
methodWithErrorLoading,
methodWithErrorError,
methodWithErrorResult,
},
undefined,
2
)}
</pre>
</div>
</div>
);
};
38 changes: 38 additions & 0 deletions example/imports/ui/react-meteor-method-hooks/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export type UseMeteorCallHookConfig = {
/** Callback to be executed after the method is called */
cb?: (error: object | undefined, result: any) => void;

/** Validate params before handler execution */
validate?: (...params: any[]) => boolean;
/** Transform params after validation before handler execution */
transform?: (...params: any[]) => any[];

/** Forces to use Meteor.call() instead of Meteor.callAsync() */
forceSyncCall?: boolean;
/** Adds some logging in console */
logging?: boolean;
/**
* By default the package logs console.error for all incoming errors
* The setting disables such logs
*/
suppressErrorLogging?: boolean;
};

export type UseMeteorCallHookResult = [
methodHandler: (...params: any[]) => Promise<any>,
loading: boolean,
error: object | undefined,
result: any
];

export type UseMeteorCallHookState = {
loading: boolean;
error?: object;
result: any;
};

export function useMeteorCall(
methodName: string,
meteorCallHookConfig?: UseMeteorCallHookConfig,
...methodParams: any[]
): UseMeteorCallHookResult;
Loading