Giter Site home page Giter Site logo

jul-sh / hook-into-props Goto Github PK

View Code? Open in Web Editor NEW
47.0 47.0 0.0 1.95 MB

Tiny HoC to use React hooks with class components.

Home Page: https://www.npmjs.com/package/hook-into-props

License: MIT License

JavaScript 100.00%
codesandbox higher-order-component hooks javascript react

hook-into-props's Introduction

—ฅ/ᐠ. ̫ .ᐟ\ฅ —

Juliette says hi.

hook-into-props's People

Contributors

dependabot[bot] avatar jul-sh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

hook-into-props's Issues

Consider filtering props

The helper currently exposes a way to utilize the component's props & add props to the component.

const useHooks = props => {
  const [isLoading, searchResults] = useFetch(
    `https://foo.com/?search=${props.searchTerm}`
  )

  return { isLoading, searchResults }
}

export default hookIntoProps(useHooks)(SearchResults)

In this case, props.searchTerm may not be required in the actual SearchResults component. What if props were passed through (instead of just to) the function passed to hookToProps? This would allow filtering:

const useHooks = ({ searchTerm, componentProps }) => {
  const [isLoading, searchResults] = useFetch(
    `https://foo.com/?search=${searchTerm}`
  )

  return { ...componentProps, isLoading, searchResults }
}

export default hookIntoProps(useHooks)(SearchResults)

Seems nice, but would require doing this at all times, even if useHooks does not use props itself:

class DisplayWindowSize extends React.Component {
  // ...
  render() {
    this.props.windowSize
  }
}

const useHooks = props => ({ ...props, windowSize: useWindowSize() }))

export default hookIntoProps(useHooks)(DisplayWindowSize)

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.