Code Monkey home page Code Monkey logo

frontend-essentials's Introduction

React Components

If

Is meant to replace short-circuit evaluation conditional rendering:

Instead of:

{userLoggedIn && !loading && <UserModal />}

Do:

<If condition={userLoggedIn && !loading}>
    <UserModal />
</If>

LazyRender

Lazily renders a large list of items:

<LazyRender items={notes} batch={20}>
    {({ _id, title, content, date }) => (
        <Note
            key={_id}
            title={title}
            content={content}
            date={date}
        />
    )}
</LazyRender>

Media

Conditionally renders a component when certain media queries are matched:

<Media query="(max-width: 768px)">
    <Menu />
</Media>

When an array is passed to query, queries will have an OR relationship when matching:

// matches when width is at most 480px or at least 1200px
<Media query=["(max-width: 480px)", "(min-width: 1200px)"]>
    <Menu />
</Media>

React Hooks

useAxios

Handles async requests easily:

const { loading, status, error, data, activate } = useAxios({
    method: 'get',
    url: 'https://example.com/v1/items',
    onSuccess: ({ data }) => console.log(data)
})

Initial request can be skipped and triggered manually:

const { loading, status, error, data, activate } = useAxios({
    method: 'get',
    url: 'https://example.com/v1/items',
    manual: true,
    onError: ({ error }) => console.error(error)
})

setTimeout(activate, 3000)

Get axios instance:

import { axios } from 'frontend-essentials'

axios.defaults.withCredentials = true

useProgressiveImage

Returns a low quality image source until a higher resolution image is fetched:

const [src, blur] = useProgressiveImage(lowQualitySrc, highQualitySrc)

return <img style={{filter: blur ? 'blur(20px)' : 'none'}} src={src} />

useViewport

Returns a viewport object containing boolean matches for each passed media query:

const { mobile, tablet } = useViewport({
    mobile: '(max-width: 480px)',
    tablet: '(min-width: 481px) and (max-width: 1199px)'
})

const getDescription = () => {
	if (mobile) return 'Hello'
	if (tablet) return 'Hello Mr.'
	return 'Hello Mr. Smith'
}

return <div>{getDescription()}</div>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.