Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Native what's Next?

React Native what's Next?

It’s almost the end of 2018 and while we’re waiting for 2019 to come, let’s prepare ourselves for the changes that are happening in React and React Native ecosystem in the following year.

Core teams from both technologies have introduced very interesting roadmaps. A lot of good stuff is expected, some has already come unnoticed, some is yet to come.

Pavlo Babenko

December 13, 2018
Tweet

More Decks by Pavlo Babenko

Other Decks in Technology

Transcript

  1. React Native Next import React, { Component } from 'react';

    import { TouchableOpacity, Text } from 'react-native'; class Button extends Component { render() { const { text, onPress } = this.props; return <TouchableOpacity onPress={onPress}> <Text>{text}</Text> </TouchableOpacity>; } }
  2. React Native Next import React, { PureComponent } from 'react';

    import { TouchableOpacity, Text } from 'react-native'; class Button extends PureComponent { render() { const { text, onPress } = this.props; return <TouchableOpacity onPress={onPress}> <Text>{text}</Text> </TouchableOpacity>; } }
  3. React Native Next import React from 'react'; import { TouchableOpacity,

    Text } from 'react-native'; const Button = React.memo(function Button(props) { const { text, onPress } = this.props; return <TouchableOpacity onPress={onPress}> <Text>{text}</Text> </TouchableOpacity>; });
  4. React Native Next import React from 'react'; const HeavyComponent =

    React.lazy(() => import('./HeavyComponent')); function RegularComponent() { return ( <React.Suspense fallback={<LoadingIndicator/>} > <View> <HeavyComponent /> </View> </React.Suspense> ); }
  5. React Native Next import React from 'react'; import { TouchableOpacity,

    Text } from 'react-native'; function HookedComponent() { const [isLightsOn, setLights] = useState(false); return ( <TouchableOpacity onPress={() => setLights(!isLightsOn)}> <Text>Turn lights {isLightsOn ? 'Off' : 'On'}</Text> </TouchableOpacity> ); }
  6. React Native Next function HookedComponent() { const [isLightsOn, setLights] =

    useState(false); return ( <TouchableOpacity onPress={() => setLights(!isLightsOn)}> <Text>Turn lights {isLightsOn ? 'Off' : 'On'}</Text> </TouchableOpacity> ); } class NotHookedComponent extends React.Component { state = { isLightsOn: false }; toggleLights = () => { const { isLightsOn } = this.state; this.setState({isLightsOn: !isLightsOn}); } render() { const { isLightsOn } = this.state; return ( <TouchableOpacity onPress={this.toggleLights}> <Text>Turn lights {isLightsOn ? 'Off' : 'On'}</Text> </TouchableOpacity> ); } }
  7. React Native Next function HookedComponent() { const [isLightsOn, setLights] =

    useState(() => { waitForItFunction(); return false; }); return ( <TouchableOpacity onPress={() => setLights(!isLightsOn)}> <Text>Turn lights {isLightsOn ? 'Off' : 'On'}</Text> </TouchableOpacity> ); }
  8. React Native Next useState, that’s all? • useState • useEffect

    • useContext • useReducer • useCallback • useMemo • useRef • useImperativeMethods • useLayoutEffects
  9. React Native Next function Comments(props) { const { postId }

    = props const [comments, setComments] = useState([]); useEffect(() => { // componentDidMount / componentWillUpdate subscribeToCommentsFor(postId, setComment); // componentWillUnmount return () => { unsubscribeFromCommentsFor(postId); } }, [postId]); return <FlatList data={comments} />; }
  10. React Native Next function Comments(props) { const { postId }

    = props const [comments, setComments] = useState([]); useEffect(() => { subscribeToCommentsFor(postId, setComments); return () => { unsubscribeFromCommentsFor(postId); } }, [postId]); return <FlatList data={comments} />; } class Comments extends React.Component { state = { comments: [] }; setComments = (comments) => { this.setState({comments}); } componentWillMount() { const { postId } = this.props; subscribeToCommentsFor(postId, this.setComments); } componentWillReceiveProps(nextProps) { const { postId } = nextProps; subscribeToCommentsFor(postId, this.setComments); } componentWillUnmount() { unsubscribeFromCommentsFor(this.props.postId); } render() { return <FlatList data={this.state.comments}/>; } }
  11. React Native Next const initialState = { count: 0 };

    function reducer(state, action) { switch (action.type) { case 'reset': return initialState; case 'increment': return { count: state.count + 1 }; case 'decrement': return { count: state.count - 1 }; default: return state; } } function Counter({ initialCount }) { const [state, dispatch] = useReducer(reducer, { count: initialCount }); return ( <Fragment> <Text>Count: {state.count}</Text> <Button text="Reset" onPress={() => dispatch({ type: 'reset' })} /> <Button text="+" onPress={() => dispatch({ type: 'increment' })} /> <Button text="-" onPress={() => dispatch({ type: 'decrement' })} /> </Fragment> ); }
  12. React Native Next function checkPostingPermissions() { const [canPost, setCanPost] =

    useState(false); function handlePermissions(permissions) { setCanPost(permissions.writer); } useEffect(() => { const userPermissions = fetchCurrentUserPermissions(); handlePermissions(userPermissions); }); return canPost; } function FeedInput() { const [text, setText] = useState(""); const canPost = checkPostingPermissions(); return canPost ? <TextInput onChangeText={setText} /> : null; } function CommentInput() { const [text, setText] = useState(""); const canPost = checkPostingPermissions(); return canPost ? <TextInput onChangeText={setText} /> : null; }
  13. React Native Next // 1. Part of an app (not

    final API) <React.unstable_ConcurrentMode> <Something /> </React.unstable_ConcurrentMode> // 2. Whole app (not final API) ReactDOM.unstable_createRoot(domNode).render(<App />);
  14. React Native Next function Counter({ initialCount }) { const [state,

    dispatch] = useReducer(reducer, { count: initialCount }); return ( <Text>Count: {state.count}</Text> <Button text="Reset" onPress={() => dispatch({ type: 'reset' })} /> <Button text="+" onPress={() => dispatch({ type: 'increment' })} /> <Button text="-" onPress={() => dispatch({ type: 'decrement' })} /> ); }
  15. React Native Next function Counter({ initialCount }) { const [state,

    dispatch] = useReducer(reducer, { count: initialCount }); return ( <View> <Text>Count: {state.count}</Text> <Button text="Reset" onPress={() => dispatch({ type: 'reset' })} /> <Button text="+" onPress={() => dispatch({ type: 'increment' })} /> <Button text="-" onPress={() => dispatch({ type: 'decrement' })} /> </View> ); }
  16. React Native Next function Counter({ initialCount }) { const [state,

    dispatch] = useReducer(reducer, { count: initialCount }); return ( <React.Fragment> <Text>Count: {state.count}</Text> <Button text="Reset" onPress={() => dispatch({ type: 'reset' })} /> <Button text="+" onPress={() => dispatch({ type: 'increment' })} /> <Button text="-" onPress={() => dispatch({ type: 'decrement' })} /> </React.Fragment> ); }
  17. What is Layout-only views? React Native Next - Pure <View>

    (not a subclass) - No associated event listeners - No associated `nativeId` - Not accessible - Overflow visible - Full opacity - No background color - No transform - No shadow - No zIndex