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.

🎣 Using React Hooks in React Native

See original GitHub issue

React Hooks will ship as part of React Native 0.59.

If you are interested in using Hooks before then, see my comment below.

Original issue:

Environment

[skip envinfo] OS: Windows 10 Node: 10.11.0 Yarn: Not Found npm: 6.4.1 Watchman: Not Found Xcode: N/A Android Studio: Version 3.2.0.0 AI-181.5540.7.32.5056338

Packages: (wanted => installed) react: 16.7.0-alpha.0 => 16.7.0-alpha.0 react-native: https://github.com/expo/react-native/archive/sdk-30.0.0.tar.gz => 0.55.4 expo: 2.2.0

Description

When using React Hooks like useState within a function component, I get the following error message.

hooks can only be called inside the body of a function component

export default function RootContainer(props) {
  const [something, setSomething] = useState('nothing');

  return (
    <View />
  );
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:50
  • Comments:133 (69 by maintainers)

github_iconTop GitHub Comments

146reactions
hramoscommented, Nov 9, 2018

Using Hooks in React Native

If you’d like to try out Hooks today, you can do the following after installing React 16.7.0-alpha in your project:

Note: Hooks are an experimental proposal to React. You can learn more in @gaearon’s post, Making Sense of React Hooks.

  1. Clone the https://github.com/facebook/react repository.
  2. Navigate to the react directory.
  3. Replace all instances of enableHooks = false with enableHooks = true in packages/shared.
  4. Run yarn install
  5. Run yarn build -- --type=RN_OSS.
  6. After a few seconds, you’ll notice a new build/react-native/ directory has been created. Copy the contents of this directory, to your project’s node_modules/react-native/Libraries/Renderer directory.

Let us know if the above steps work out for you.


We’re planning on landing support for React 16.6.0 in 0.57.5. Hooks is available in the 16.7.0-alpha, and feedback from the community so far has been that upgrading to alphas of React in the stable React Native releases can be disruptive. What do people here think about a React Native 0.57.5-alpha release that includes support for 16.7.0-alpha out of the box?

48reactions
gaearoncommented, Oct 28, 2018

We can just do a sync with enableHooks: false feature flag and then those who want can override it locally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks with React & React Native ⚓️ | by Sohad Dader
Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don't work inside classes — they...
Read more >
Introducing Hooks - React
With Hooks, you can extract stateful logic from a component so it can be tested independently and reused. Hooks allow you to reuse...
Read more >
Production-Level Patterns for React Hooks 🎣🦈 - FullStack Labs
Using hooks incorrectly can easily cause “unnecessary” re-renderings. Although this has become a common concern surrounding hooks, it can be mitigated by ...
Read more >
How to Build a React Native App Using React Native Hooks?
Build React Native app using React Native Hooks in this step-by-step tutorial. Understand what Hooks are and know the basic types of React...
Read more >
Hooks with React & React Native ⚓️ - LinkedIn
Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don't work inside classes — they ......
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

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