question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React hooks & context

See original GitHub issue

I’d love to be able to use react hooks in react-blessed.

It’s an RFC, now, but looks like it will probably solidify into mainline react, soon. In order to test, I installed react@16.7.0-alpha.2.

As it is, I get this error:

Invariant Violation: Hooks can only be called inside the body of a function component.

with this code:

import React, { useState, useEffect } from 'react'
import fetch from 'node-fetch'

function App () {
  const [posts, setPosts] = useState([])
  useEffect(async () => {
    const items = await fetch(
      'https://jsonplaceholder.typicode.com/posts'
    ).then(r => r.json())
    setPosts(items.map(i => i.title))
  })

  return (
    <list items={posts} />
  )
}

To verify that the hooks work, etc, here is a similar thing in a sandbox, in react-dom.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Yomguitherealcommented, Nov 18, 2018

Hello @konsumer. I agree it would be nice to have hooks support. Unfortunately I am not even sure the current codebase is able to work with react@16 since a lot of changes were made to the reconciler logic. I have not much time to work on this but will of course consider any PR.

0reactions
konsumercommented, Nov 19, 2018

Ah, I got it. needed mouse for buttons to work. Ok, ready for review.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks API Reference - React
This page describes the APIs for the built-in Hooks in React. ... If you're familiar with the context API before Hooks, useContext(MyContext) is...
Read more >
React useContext Hook - W3Schools
React Context is a way to manage state globally. It can be used together with the useState Hook to share state between deeply...
Read more >
A Guide to React Context and useContext() Hook
The context in React is a concept that lets you supply child components with global data, no matter how deep they are in...
Read more >
How and When to Use Context in React with Hooks
Context allows you to pass data across any number of React components, regardless of nesting. Redux or Context? In a very small application,...
Read more >
How to useContext in React - Robin Wieruch
React's useContext Hook takes the Context as parameter to retrieve the value from it. Using the React Hook instead of the Consumer component ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found