Skip to content

Instantly share code, notes, and snippets.

View mohsentaleb's full-sized avatar
😎

Mohsen Taleb mohsentaleb

😎
View GitHub Profile
@mohsentaleb
mohsentaleb / mui_theme_inside_function_body.jsx
Created April 12, 2020 19:16
Using MUI Theme Variable In Function Body
import { Box } from "@material-ui/core"
import { useTheme } from "@material-ui/styles"
function myComponent() {
const theme = useTheme()
return <Box padding={theme.spacing(4)} />
}
@mohsentaleb
mohsentaleb / mui_theme_inside_function_body.tsx
Created April 12, 2020 19:15
Using MUI Theme Variable In Function Body
import { Box, Theme } from "@material-ui/core"
import { useTheme } from "@material-ui/styles"
function myComponent() {
const theme = useTheme<Theme>()
return <Box padding={theme.spacing(4)} />
}
@mohsentaleb
mohsentaleb / mui_component_style_override.jsx
Created April 12, 2020 19:11
mui_component_style_override.jsx
import { Avatar, makeStyles } from "@material-ui/core"
const useAvatarStyles = makeStyles(
theme => {
colorDefault: {
// overriding `colorDefault` according to the Avatar API
backgroundColor: theme.palette.secondary
}
},
{ name: "MuiAvatar" }
@mohsentaleb
mohsentaleb / mui_component_style_override.tsx
Last active April 12, 2020 19:12
Overriding MUI Component Styles
import { Avatar, makeStyles, Theme, createStyles } from "@material-ui/core"
const useAvatarStyles = makeStyles(
(theme: Theme) =>
createStyles({
colorDefault: {
// overriding `colorDefault` according to the Avatar API
backgroundColor: theme.palette.secondary
}
}),
@mohsentaleb
mohsentaleb / mui_custom_styles_function_body.jsx
Last active April 12, 2020 19:12
Creating New Custom Styles (classes) For Using In Function Body
import { makeStyles } from "@material-ui/core"
const useStyles = makeStyles(theme => ({
myCustomClass: {
color: theme.palette.text.primary
}
}))
function myComponent() {
const classes = useStyles()
@mohsentaleb
mohsentaleb / mui_custom_styles_function_body.tsx
Last active April 12, 2020 19:12
Creating New Custom Styles (classes) For Using In Function Body
import { makeStyles, createStyles, Theme } from "@material-ui/core"
const useStyles = makeStyles((theme: Theme) =>
createStyles({
myCustomClass: {
color: theme.palette.text.primary
}
})
)
@mohsentaleb
mohsentaleb / promise_all_vs_loop.ts
Last active April 9, 2020 20:20
Promise.all vs Iterating thought loop
const hero = {
superman: {
name: "Superman",
alias: "Clark Kent"
},
batman: {
name: "Batman",
alias: "Bruce Wayne"
},
flash: {
const sleep = (ms: number, dev: number = 1) => {
const msWithDev = (Math.random() * dev + 1) * ms
console.log('Sleeping', msWithDev / 1000, 'sec')
return new Promise(resolve => setTimeout(resolve, msWithDev))
}
async function fetch() {
await sleep(1000)
}
import React, { Component } from "react"
import _ from 'lodash'
import hash from 'object-hash'
class Listings extends Component {
sortByIndex = (a, b, index, ascending) => (ascending ? a[index] - b[index] : b[index] - a[index])
sortListings = _.memoize((listings, index, ascending) =>
listings.sort((a,b) => this.sortByIndex(a, b, index, ascending)),
(...args) => `${hash(listings)}_${args[1]}_${args[2]}`) // Makes a hash in format: "objectHashId_beds_true"
import React, { Component } from "react";
import _ from 'lodash'
class Listings extends Component {
sortByIndex = (a, b, index, ascending) => (ascending ? a[index] - b[index] : b[index] - a[index])
sortListings = _.memoize((listings, index, ascending) =>
listings.sort((a,b) => this.sortByIndex(a, b, index, ascending)))
render() {