Hooks for React Native web and Expo
Hooks for implementing complex functionality in React Native for web and Expo.
A closer look at how the hooks work here.
yarn add react-native-web-hooks
or
npm install --save react-native-web-hooksImport the library into your JavaScript file:
import {
useDimensions,
useActive,
useFocus,
useHover,
useREM,
useScaledSize,
} from 'react-native-web-hooks';Use these in place of rem font sizes like: font-size: 1.3rem.
Note: this isn't a hook anymore and will be moved out in the future.
const fontSize = useREM(1.3);
return ;These change based on the width of the screen.
const fontSize = useScaledSize(1.5);
return ;Note that fontScale is hard-coded to 1 on the react-native-web side and shouldn't be used to calculate dynamic font sizes.
const {
window: { width, height, fontScale, scale },
screen,
} = useDimensions();It's best to style a view based on that own view's size and not the window size. To make this easier you can use the useLayout hook!
Using
onLayoutmay require you to installresize-observer-polyfill. Learn more in the official Expo docs
const {
onLayout,
width,
height,
x,
y
} = useLayout();
return These will be replaced by React Flare when it's released.
…Import the library into your JavaScript file:
import { Hoverable, Resizable } from 'react-native-web-hooks';You can wrap a function or a component.
import React, { Component } from 'react';
import { Text, TouchableOpacity, View } from 'react-native';
import { Hoverable } from 'react-native-web-hooks';
const createLogger = (...msg) => () => {
console.log(...msg);
};
class App extends Component {
render() {
return (
{isHovered => (
Welcome to React}
)}
);
}
}Observe window resize events.
return (
{layout => }
);No open issues yet, or sync has not completed.