Code Monkey home page Code Monkey logo

iota's Introduction

๐Ÿ’ซ Iota

Manage stateful logic with class methods like the good ol' times, but with reuable hooks.

Install

npm install iota-hook

Quickstart

Create a ususable instance of iota to grab a remote api data via debounce from incoming props

import createIota from 'iota-hook'
import debounce from 'lodash.debounce'
import searchQuery from './searchQuery'

interface LocationSearchProps {
  query: string
}
interface LocationSearchState {
  searchResults: string[]
}

interface LocationCustomMethods = {
  search: (query: string) => Promise<void>
}

interface LocationSearchResults = {
  results: { city: string, country: string }[]
  amount: number
}


const useLocationHook = createIota<LocationSearchProps, LocationSearchState, LocationCustomMethods, LocationSearchResults>({
  init: (self) => {
    self.state = { searchResults: [] }
  },
  didUpdate: (self) => {
    const { props, prevProps } = self
    if (props.query !== prevProps.query) {
      self.search(props.query)
    }
  },
  didMount: (self) => {
    self.search = debounce(self.search, 1000)
  },
  search: async (self, arg) => {
    const results = await searchQuery(arg)
    self.setState({ searchResults: results })
  },
  render: (self) => {
    const { state } = self

    return {
      results: state.searchResults.map((location) => {
        const [city, country] = location.split(', ')
        return { city, country }
      }),
      amount: state.searchResults.length
    }
  }
})

Then you can reuse this in any component

import useLocationHook from './useLocationHook'

function App () {
  const [searchText, setSearchText] = React.useState('')

  const data = useLocationHook({ query: searchText })

  console.log({ data }) // data is data created from render method in iota hook

  return (
    <div>
      <input type='text' onChange={e => setSearchText(e.target.value)} />
    </div>
  )
}

iota's People

Contributors

alnorris avatar

Watchers

 avatar

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.