Skip to content

Instantly share code, notes, and snippets.

View alpavlove's full-sized avatar

Alexey Pavlov alpavlove

View GitHub Profile
@alpavlove
alpavlove / Button.tsx
Created July 28, 2022 13:24
Create React Components library
import React, { ReactNode } from 'react'
import './Button.css'
export type ButtonProps = {
onClick(): void
children: ReactNode
variant?: 'primary' | 'success'
isDisabled?: boolean
}
@alpavlove
alpavlove / storybook.yml
Created July 21, 2022 08:30
Deploy storybook to Github Pages workflow
name: Storybook
on:
push:
paths: ["stories/**", "src/**"]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/[email protected]
@alpavlove
alpavlove / demo.tsx
Created May 23, 2022 11:28
React Hooks instead of lifecycle methods
useEffect(() => {
console.log(title)
}, [title]) // Re-run the effect only if title was changed
@alpavlove
alpavlove / demo.tsx
Created May 23, 2022 11:27
React Hooks instead of lifecycle methods - useEffect hook instead of componentWillUnmount
useEffect(() => {
// to do smth...
return () => {
// to do smth else when a component is unmounted...
}
})
@alpavlove
alpavlove / demo.tsx
Last active May 23, 2022 11:28
React Hooks instead of lifecycle methods - useEffect hook instead of componentDidMount
//componentDidMount and componentDidUpdate. An effect re-runs every render
useEffect(() => {
// to do smth...
})
//componentDidMount. An effect runs only on mount
useEffect(() => {
// to do smth...
@alpavlove
alpavlove / demo.tsx
Last active May 23, 2022 11:34
React Hooks instead of lifecycle methods
const Component = React.memo(function Component(props) {
// your code is here...
})
@alpavlove
alpavlove / Tabs.tsx
Created May 23, 2022 10:57
Basic Tabs component - Tabs component with logic Tabs.tsx
import React, { ReactElement, useState } from "react"
import TabTitle from "./TabTitle"
type Props = {
children: ReactElement[]
}
const Tabs: React.FC<Props> = ({ children }) => {
const [selectedTab, setSelectedTab] = useState(0)
@alpavlove
alpavlove / TabTitle.tsx
Last active August 2, 2023 16:46
Basic Tabs component - Tabs component with logic
import React from "react"
type Props = {
title: string
index: number
setSelectedTab: (index: number) => void
}
const TabTitle: React.FC<Props> = ({ title, setSelectedTab, index }) => {
@alpavlove
alpavlove / App.tsx
Created May 23, 2022 10:54
Basic Tabs component - App.tsx
import React from "react"
import Tabs from "../Tabs"
import Tab from "../Tabs/Tab"
function App() {
return (
<Tabs>
<Tab title="Lemon">Lemon is yellow</Tab>
<Tab title="Strawberry">Strawberry is red</Tab>
<Tab title="Pear">Pear is green</Tab>
@alpavlove
alpavlove / TabTitle.tsx
Last active May 23, 2022 10:59
Basic Tabs component - TabTitle.tsx
import React from "react"
type Props = {
title: string
}
const TabTitle: React.FC<Props> = ({ title}) => {
return (
<li>