Socket
Socket
Sign inDemoInstall

tasook

Package Overview
Dependencies
8
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tasook

Create tasks to perform effect easier with React Hooks


Version published
Maintainers
1
Install size
31.8 kB
Created

Readme

Source

tasook

Create tasks to perform effect easier with React Hooks

NPM JavaScript Style Guide

Install

npm install --save tasook

Overview

React Hooks provide useEffect for perform side effects, but we need :

  1. Reduce the boilerplate code when handling async operations
  2. Solution to de-couple the trigger & consume of effects, to avoid consume logics in useEffect function, like Dan mentioned in he's twitter

So we build createTask & useTask, to enable us consume data first, then trigger the async operation when need.

Usage

import React, { useEffect } from 'react'
import { effectA, effectB } from './effects';
import { createTasks, createTask, useTask } from 'tasook'

const taskA = createTask(effectA)
const tasks = createTasks({ // create multiple tasks
  effectB,
})

const App = () => {
  // consume task with useTask
  const [dataA, errorA, loadingA] = useTask(taskA)
  const [dataB] = useTask(tasks.effectB)

  useEffect(() => { 
    // only trigger effects in useEffect, do not consume effect results here
    taskA('John') // taskA will pass all arguments to effectA
    tasks.effectB('Kpax')
  }, [])

  return (
    <div>
      hello Tasook
      <div>
        effectA: {dataA}
        <br/>
        effectB: {dataB}
      </div>
      <p>
        <button
          disabled={loadingA}
          onClick={
            () => {
              taskA('Mark@' + (new Date()))
                .then(()=> {
                  console.log('taskA returns what effectA returns')
                })   
            }
          }
        >
          {loadingA ? 'Loading...' : 'Reload effectA'}
        </button>
      </p>
    </div>
  )
}

export default App

License

MIT © kpaxqin

FAQs

Last updated on 24 Feb 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc