npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hooker-boilerplate

v0.1.2

Published

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

Downloads

6

Readme

This project was bootstrapped with Create React App.

React hook boilerplate is redux like application but no redux

You could use this quickly setup for react hook working with context api and use multiply reducer applications. No more redux needed anymore.

Sample

In the project directory, you can run:

npm start

The Setup

Make sure you have Nodejs installed on your machine (I’m using version v12.6.0) and we’ll put the app together using, create-react-app: $ npx create-react-app react-hooker-boilerplate

Once it’s done, start it by running $ npm start in your project directory. Then open the package.json file. require node version 16.8 above

The "react-hooker-boilerplate" will follow Redux three fundamental principles, stores, actions, and reducers. But build by useing context api working with useReducer

npm i react-router-dom

  • let's start from create a store first, I have create configureStore.js under store folder
import React, {createContext, useContext, useReducer} from 'react';

import rootReducer, { initialState } from '../reducers/index';

const StateContext = createContext();
const DispatchContext = createContext();

export const useStateValue = () => useContext(StateContext);
export const useDispatch = () => useContext(DispatchContext);

function Store(props) {
    
    const [state, dispatch] = useReducer(rootReducer, initialState);
    
    return (
       <DispatchContext.Provider value={dispatch}>
        <StateContext.Provider value={state}>
           {props.children}
        </StateContext.Provider>
      </DispatchContext.Provider>
    );
  }


export default Store;
  • since application grow will handle more and more complex state, so I create two separate reducer as example how to handle them

  • userReducer


import { UPDATE_SELECTED_USER } from '../constants/actionConstants';


const userReducer = (state, action) => {
    switch (action.type) {
        case UPDATE_SELECTED_USER: 
            return {
                ...state,
                selected: action.selected
            }
  
        default:
            return state;
    }
  }
  
  export default userReducer;
  • bookReducer
    import { UPDATE_SELECTED_BOOK } from '../constants/actionConstants';

    const bookReducer = (state, action) => {
    switch (action.type) {
        case UPDATE_SELECTED_BOOK:
            return {
                ...state,
                selected: action.selected
            }

        default:
        return state;
    }
    }

    export default bookReducer;
  • rootReducer
    import bookReducer from './bookReducer';
    import userReducer from './userReducer';

    const rootReducer = ({book, user}, action) => {
        // middleware goes here, i.e calling analytics service, etc.
    
        return {
            book: bookReducer(book, action),
            user: userReducer(user, action)
        }
    }

    export const initialState = {
        user: {
            list: [
                {
                "balance": "$3,946.45",
                "picture": "http://placehold.it/32x32",
                "age": 23,
                "name": "Bird Ramsey",
                "gender": "male",
                "company": "NIMON",
                "email": "[email protected]"
                },
            ....
            
                ],
        selected: null
        },
        book: {
        selected: null,
        list: [
            {
            "author": "Chinua Achebe",
            "country": "Nigeria",
            "imageLink": "images/things-fall-apart.jpg",
            "language": "English",
            "link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
            "pages": 209,
            "title": "Things Fall Apart",
            "year": 1958
            },
            ...
            ]
        }
    }

    export default rootReducer;
  • action that work exactly like redux
    import { UPDATE_SELECTED_USER } from  '../constants/actionConstants';

    export const updateSelectedUser = (dispatch, user) => 
        dispatch ({
            type: UPDATE_SELECTED_USER,
            selected: user
        })
  • UserComponent build in react hook and reader data from your stor and fire action
    import React from 'react'
    import { useStateValue, useDispatch } from '../../store/configureStore';
    import { updateSelectedUser } from '../../reducers/user.action';

    const UserInfo = ({user, selected, onClick}) => {

        const handleClick = (user) => e => {
            onClick(user);
        };

        return (
        <tr 
            onClick={handleClick(user)} 
            className={`${user === selected ? 'hightlight' : '' }`}
        >
            <td>{user.balance}</td>
            <td><img src={user.picture} width={60} height={80}/></td>
            <td>{user.age}</td>
            <td>{user.name}</td>
            <td>{user.gender}</td>
            <td>{user.company}</td>
            <td>{user.email}</td>
        </tr>
        )


    }


    export default function UserComponent() {
        const {user} = useStateValue();

        const {list, selected} = user;

        const dispatch = useDispatch();

        const handleOnClick = user => updateSelectedUser(dispatch, user);
        
        return (
            <table>
            <tbody>
            <tr>
                <th>balance</th>
                <th>picture</th>
                <th>age</th>
                <th>name</th>
                <th>gender</th>
                <th>company</th>
                <th>email</th>
            </tr>

            {list.map( (item, idx) => <UserInfo key={idx} user={item} selected={selected} onClick={handleOnClick} /> )}
            </tbody>
        </table>
        )
    }
  • appRouter.js

import React from 'react';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

import {
    BookComponent,
    UserComponent,
    Home
} from './components/';


const  AppRouter = () => {
    return (
        <Router>
            <div>

            <ul>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/user">User</Link>
          </li>
          <li>
            <Link to="/book">Book</Link>
          </li>
        </ul>

        <hr />
            
           

            <Route exact path="/" component={Home} />
            <Route path="/user" component={UserComponent} />
            <Route path="/book" component={BookComponent} />
            </div>
            
        </Router>
    )
}

export default AppRouter;

  • App.js

import React from 'react';
import AppRouter from './AppRouter';
import './App.css';
import Store from './store/configureStore';

function App() {
  return (
    <Store>
      <AppRouter/>
   </Store>
  );
}

export default App;

  • middleware that could be build a separeat file and put it into my rootReduce
const rootReducer = ({book, user}, action) => {
    // middleware goes here, i.e calling analytics service, etc.
   
    return {
        book: bookReducer(book, action),
        user: userReducer(user, action)
    }
}

Conclusion

As you can see with the power of the context api and react hooks it can entirely get rid of redux but working like redux application.