Skip to content

Instantly share code, notes, and snippets.

@SangeetAgarwal
SangeetAgarwal / useDebounce.js
Created October 26, 2020 00:53
debounce as xhr call using hooks
import React from "react";
const useDebounce = (callbackFn: () => any | Promise<any> , timeout: number = 500) => {
const [sends, setSends] = React.useState(0);
const debounceRequest = () => {
setSends(sends + 1);
};
React.useEffect(() => {
@SangeetAgarwal
SangeetAgarwal / SWR to fetch data
Created September 5, 2020 15:24
use swr to fetch data & pass to presentation layer
const ResourceEditWrapper = () => {
const query = useQuery();
const classes = useStyles({});
// Resource
const {
resource,
isLoading: isResourceLoading,
React.useEffect(() => {
setLoading(true);
const source = axios.CancelToken.source();
async function fetchData() {
try {
// Get all the Category content areas & topics
const dataAccess = new DataAccess.Client(props.user);
@SangeetAgarwal
SangeetAgarwal / avoid-deep-merge.js
Last active August 23, 2018 14:28
avoid-deep-merge
updateState({target}) {
this.setState(prevState => {
return {
user: {
...prevState.user,
[target.name]: target.value,
address: {
// first lay out all the properties within the address object
...prevState.user.address,
// now, overwrite the properties that need to be overwritten...
@SangeetAgarwal
SangeetAgarwal / callbackFormOfSetState.js
Last active August 23, 2018 12:03
callbackFromOfSetState
updateState({target}) {
this.setState((prevState) => {
const updatedUser = {...prevState.user, [target.name]: target.value}; // use previous value in state to build new state...
return { user: updatedUser }; // And what I return here will be set as the new state
}, () => this.doSomething(this.state.user); // Now I can safely utilize the new state I've created to call other funcs...
);
}

WebStorm

emoji - Ctrl+Cmd+Space MacOS

multiselect - Ctrl + G MacOS

int main()
{
return 0;
}
class Fraction
{
public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class
{
T item = MemoryCache.Default.Get(cacheKey) as T;
int duration;
if (ConfigurationManager.AppSettings["InMemoryCacheDuration"] == null ||
!Int32.TryParse(ConfigurationManager.AppSettings["InMemoryCacheDuration"], out duration))
duration = 30;
@SangeetAgarwal
SangeetAgarwal / dataservice.js
Last active August 29, 2015 14:07
Revealing module pattern for implementing a service in Angular.js
.factory("dataService", ["$http", "$q", function ($http, $q) {
var _ratingEntities = [];
var _getRatingEntities = function (options) {
var deferred = $q.defer();
$http.get("api/RatingEntityProfileApi?currentPage=" + options.currentPage + "&" + "recordPerPage=" + options.recordsPerPage
+ "&" + "sortKey=" + options.sortKey + "&"