Skip to content

Instantly share code, notes, and snippets.

View troygoode's full-sized avatar
🤓
Building nerdy stuff.

Troy Goode troygoode

🤓
Building nerdy stuff.
View GitHub Profile
@troygoode
troygoode / use-state-safe.js
Last active July 27, 2021 05:44 — forked from AlpacaGoesCrazy/useSafeState.js
Hook for react state which prevents updates on unmounted components (in TypeScript)
import { useEffect, useRef, useState, Dispatch, SetStateAction } from "react";
/* originally via:
https://gist.github.com/AlpacaGoesCrazy/25e3a15fcd4e57fb8ccd408d488554d7
*/
const useStateSafe = function<T>(initialValue: T): [T, Dispatch<SetStateAction<T>>] {
const isMounted = useRef(false); // useRef to memorize if the component is mounted between renders
const [state, setState] = useState<T>(initialValue);
var mongoose = require('mongoose');
var CompanySchema = new CoreRecord({
name: String,
ips: [String]
});
CompanySchema.method 'reports', function(cb){
mongoose.model('Report').find({companyId: this._id}, cb);
};