Skip to content

Instantly share code, notes, and snippets.

View RomanChasovitin's full-sized avatar

Roman Chasovitin RomanChasovitin

  • LoopDev
  • Bishkek
View GitHub Profile
const api = {
posts: "https://jsonplaceholder.typicode.com/posts"
};
export async function request<T>(url: string): Promise<T> {
const response = await fetch(url);
const body = await response.json();
return body;
}
// action types
enum CounterActionTypes {
increment = 'increment',
decrement = 'decrement'
}
// interfaces & types
interface CounterState {
value: number
}
@RomanChasovitin
RomanChasovitin / .js
Created May 5, 2021 10:53
React+Typescript article > simple TS component
import React, { useState } from "react";
interface IProps {
onClick: (event: React.SyntheticEvent) => void;
text: string;
}
const Button: React.FC<IProps> = ({ onClick, text }) => {
const [clickCount, setCount] = useState(0);
@RomanChasovitin
RomanChasovitin / .jsx
Last active May 5, 2021 10:43
React+Typescript article > simple js component
import React, { useState } from "react";
const Button = ({ onClick, text }) => {
const [clickCount, setCount] = useState(0);
const handleClick = (event) => {
setCount(clickCount + 1);
onClick(event);
};
class Counter = {
constructor(initial) {
this.value = initial
}
increment() {
this.value += 1
}
decrement() {
export const config = {
API_URL: process.env.REACT_APP_API_BASE_URL,
}
axios.get(config.API_URL)
@RomanChasovitin
RomanChasovitin / .js
Created March 29, 2021 13:46
setApiHeader
import { setApiHeader, api } from '../config'
async function authenticate() {
// Authorization
const response = await api.post('/auth')
// Getting token from response
const { token } = response
// Set header for the next authenticated requests
@RomanChasovitin
RomanChasovitin / .js
Created March 29, 2021 13:45
Custom hook example
import { useState } from 'react'
import { useList, useToggle } from 'react-use'
import fetchImages from './fetchImages'
const useFetchImages = ({ source }) => {
const [images, imagesActions] = useList([])
const [isLoading, toggleLoading] = useToggle(false)
const [error, setError] = useState(null)
import { saveState, loadState } from '../utils/localStorage'
const save = data => saveState(data, 'key')
const load = () => loadState('key')
import React from 'react'
import PropTypes from 'prop-types'
import TodoItem from '../TodoItem'
// prop-types
const propTypes = {
todos: PropTypes.arrayOf(PropTypes.object),
}