🎣 Using React Hooks in React Native
See original GitHub issueReact 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:
- Review the documentation: https://facebook.github.io/react-native
- Search for existing issues: https://github.com/facebook/react-native/issues
- Use the latest React Native release: https://github.com/facebook/react-native/releases
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:
- Created 5 years ago
- Reactions:50
- Comments:133 (69 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
react
directory.enableHooks = false
withenableHooks = true
inpackages/shared
.yarn install
yarn build -- --type=RN_OSS
.build/react-native/
directory has been created. Copy the contents of this directory, to your project’snode_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?
We can just do a sync with
enableHooks: false
feature flag and then those who want can override it locally.