Skip to content

Instantly share code, notes, and snippets.

View behnood-eghbali's full-sized avatar
🎯
Focusing

Behnood Eghbali behnood-eghbali

🎯
Focusing
View GitHub Profile
@behnood-eghbali
behnood-eghbali / Icon.js
Created August 12, 2018 08:19
Creating an SVG Icon System with React
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class StarIcon extends Component {
render() {
const {color, width, height} = this.props;
return (
<svg className="star" width={width} height={height} fill={color} viewBox="0 0 200 200">
<path d="M100,10 L150,140 20,50 180,50 50,140 Z" />
</svg>
@behnood-eghbali
behnood-eghbali / App.tsx
Last active March 31, 2021 15:20
React + TypeScript code samples with error/bug fixes
import React, {Component} from 'react';
// import { createBrowserHistory } from 'history';
import Chats from './Chats';
import Contacts from './Contacts';
import 'bootstrap/dist/css/bootstrap.min.css';
import './App.css';
/* Error: Property 'path' is missing in type '{}' but required in type 'Readonly<Props>'. TS2741
Error: Type 'Props' is not assignable to type 'string'.ts(2322)
@behnood-eghbali
behnood-eghbali / Get.js
Created August 30, 2019 06:25
Implementing HTTP methods (GET & POST) with json-server and React
import React, { Component } from 'react';
import './App.css';
export default class Get extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
}
interface IteratorResult<T> {
done: boolean;
value?: T;
}
@behnood-eghbali
behnood-eghbali / BlogPost.js
Created October 26, 2019 23:31
Simple Implementation of Form Inputs Using HOCs (React)
import React, { Component } from 'react';
//import TextBlock from './TextBlock';
import {BlogPostWithSubscription} from './withSubscription';
class BlogPost extends Component {
render() {
const {post, handlePostChange, handlePostSubmit} = this.props;
return (
<div className="MyApp">
@behnood-eghbali
behnood-eghbali / App.js
Created October 27, 2019 01:07
Render Props vs Component Composition (React)
import React from 'react';
import './App.css';
import MyFunctionComponent from './MyFunctionComponent';
import MyClassComponent from './MyClassComponent';
import MyAnotherClassComponent from './MyAnotherClassComponent';
function App() {
return (
<div className="App">
<MyFunctionComponent />
@behnood-eghbali
behnood-eghbali / App.js
Last active November 8, 2019 18:40
Simple Example of React Context API
import React, { Component } from 'react'
import Toolbar from './Toolbar'
import {ThemeContext, themes} from './ThemeContext'
export default class App extends Component {
constructor(props) {
super(props)
this.state = {theme: themes.light}
this.toggleTheme = this.toggleTheme.bind(this)
@behnood-eghbali
behnood-eghbali / AddTodo.js
Last active December 4, 2019 09:34
Simple Todo App Using React Context API & Material-UI
import React, { Component } from 'react';
import Paper from '@material-ui/core/Paper';
import Grid from '@material-ui/core/Grid';
import TextField from '@material-ui/core/TextField';
import InputAdornment from '@material-ui/core/InputAdornment';
import AddCircleRoundedIcon from '@material-ui/icons/AddCircleRounded';
import {TodoContext} from './TodoContext';
export default class AddTodo extends Component {
static contextType = TodoContext;
@behnood-eghbali
behnood-eghbali / multiple-delimiters.py
Last active March 16, 2020 16:08
Splitting Strings on Any of Multiple Delimiters in Python
text = "Python's built-in interfaces to operating-system services make it ideal for writing portable, maintainable system-administration tools and utilities (sometimes called shell tools)."
import re
fields = re.split(r'[;,.()?!\s]\s*', text)
fields = filter(None, fields)
print(fields)
#["Python's", 'built-in', 'interfaces', 'to', 'operating-system', 'services', 'make', 'it', 'ideal', 'for', 'writing', 'portable', 'maintainable', 'system-administration', 'tools', 'and', 'utilities', 'sometimes', 'called', 'shell', 'tools']
@behnood-eghbali
behnood-eghbali / multidict.py
Created April 25, 2020 15:09
Mapping Keys to Multiple Values in a Dictionary (Python)
# Mapping Keys to Multiple Values in a Dictionary with defaultdict (multidict)
# A dictionary is a mapping where each key is mapped to a single value. If you want to
# map keys to multiple values, you need to store the multiple values in another container
# such as a list or set.
from collections import defaultdict
first_dict = {'a': 1, 'b': 2, 'c': 3}
second_dict = defaultdict(list)
for key in first_dict: