1.2.8 • Published 3 years ago

react-use-light v1.2.8

Weekly downloads
19
License
Unlicense
Repository
github
Last release
3 years ago

react-use-light

react-use without external dependencies. Collection of essential React Hooks.

Fork from streamich/react-use

Install

npm i react-use-light

or

yarn add react-use-light

Extra Hooks

useAudioControls

Just like useAudio returns the audio element,a ref to the element and the controls. this also returns the time played "00:01" and the timeleft "02:10" in minutes.

import { useAudioControls } from 'react-use-light';

export function App(track) {
  const [{ audio, controls, state, currentAudioTimeRef, currentAudioTimeLeftRef, ref }] = useAudioControls({
    src: track.audioUrl,
    autoPlay: false,
    loop: false,
    'aria-label': track.name,
  });

  return (
    <div>
      <h1>{track.name}</h1>
      {audio}
    </div>
  );
}

useDate

import React from 'react';
import { useDate, formatDate } from 'react-use-light';

export function Test() {
  const [date] = useDate();
  return <h1>{formatDate(date)}</h1>;
}

createContextHook

import { createContext } from 'react';
import { createContextHook } from 'react-use-light';
import { API_URL } from '../constants';
import { AppContextState } from './reducer';

const initialState: AppContextState = {
  auth: {
    user: undefined,
    errorMessage: undefined,
    loading: false,
  },
  surveys: {
    openSurveys: [],
  },
};

export const AppContext = createContext(initialState);

let fetched = false;

export const useAppContext = createContextHook(AppContext, (context) => {
  async function fetchSurveys() {
    if (!fetched) {
      const res = await fetch(API_URL + '/surveys');
      const data = await res.json();
      fetched = true;
      return data.surveys;
    } else {
      console.log('returning from cache');
      return context.surveys.openSurveys;
    }
  }
  return { ...context, fetchSurveys };
});

CreateContextReducer

const [ContextProvider, useContext, Context] = createContextReducer<AppState, Actions>();
import { createContextReducer, Action } from 'react-use-light';
import { useState } from 'react';

// CREATE STATE TYPE
interface AppState {
  notes: string[];
  user: User | undefined;
}

//CREATE ACTIONS TYPE (Should extend Action interface >> {type:string, payload?:any})
type Actions = SetUserAction | SetNotesAction;

interface SetUserAction extends Action {
  type: 'SET_USER';
  payload: User | undefined;
}

interface SetNotesAction extends Action {
  type: 'SET_NOTES';
  payload: string[];
}
interface User {
  name: string;
  email: string;
}

//Pass the State Type and Actions Type to the create function in order to get a properly typed context.
// @returns >>> [ContextProvider, ContextHook, and Context]
const [AppContextProvider, useAppContext, AppContext] = createContextReducer<AppState, Actions>();

//create state reducer for the defined types
const reducer = (state: AppState, action: Actions): AppState => {
  switch (action.type) {
    case 'SET_NOTES':
      return { ...state, notes: action.payload };
    case 'SET_USER':
      return { ...state, user: action.payload };
    default:
      return { ...state };
  }
};

// initial state
const initialState: AppState = {
  notes: [],
  user: undefined,
};

// USE ContextProvider by passing initial state and the reducer function.
export function App() {
  return (
    <AppContextProvider initialState={initialState} reducer={reducer}>
      <PageOne />
    </AppContextProvider>
  );
}

// Use in ContextProvider Children
function PageOne() {
  const [{ notes }, dispatch] = useAppContext();
  const [note, setNote] = useState('');
  function addNote() {
    if (note && note.trim() !== '') {
      dispatch({
        type: 'SET_NOTES',
        payload: [...notes, note],
      });
      setNote('');
    }
  }
  return (
    <div>
      <h1>Inside Context</h1>
      <form
        onSubmit={(e) => {
          e.preventDefault();
          addNote();
        }}
      >
        <div>
          <input placeholder="add note" value={note} onChange={(e) => setNote(e.target.value)} />
        </div>
      </form>
      <div>
        {notes.map((n) => (
          <p key={n}>{n}</p>
        ))}
      </div>
    </div>
  );
}

Others

React-Use Library Documentation

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago