SlideShare ist ein Scribd-Unternehmen logo
1 von 85
Downloaden Sie, um offline zu lesen
AND INTRO TO HOOKS
Nir Hadassi
Software Engineer @ Soluto
REACT NEW FEATURES
This talk was supposed to be about...
High-Order-Components
and Recompose
But then...
And then...
@TheRealMohaz
About Myself
Nir Hadassi
4years
working at
Soluto
3years
working with
React
nirsky
What are React Hooks?
“Hooks lets you use state and
other React features without
writing a class.”
Introduced on React v16.7.0-alpha
Why?
CLASSES ARE
BAD
Why classes are bad?
● Complex components become hard to understand
● Classes confuse people (notorious this..)
● Classes confuse machines (don’t minify well)
● It’s hard to reuse stateful logic between classes
Agenda
1. Hooks Intro
a. useState
b. useRef
c. useContext
d. useEffect
2. Memo
3. Lazy
useState
Class with state
class CounterButton extends Component {
constructor() {
super();
this.state = {
count: 0
}
}
render() {
return <button onClick={() => this.setState(prevState => ({ count: prevState.count + 1
}))}>
{ this.state.count }
</button>
}
}
With useState
import React, { useState } from 'react';
const Counter = props => {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>
{ count }
</button>
}
With useState
import React, { useState } from 'react';
const Counter = props => {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>
{ count }
</button>
}
With useState
import React, { useState } from 'react';
const Counter = props => {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>
{ count }
</button>
}
With useState
import React, { useState } from 'react';
const Counter = props => {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>
{ count }
</button>
}
Multiple State Variables
const Player = props => {
const [volume, setVolume] = useState(0);
const [position, setPosition] = useState(0);
const [paused, setPaused] = useState(true);
const onClick = () => {
setPosition(0);
setPaused(false);
}
}
Multiple State Variables
const Player = props => {
const [state, setState] = useState({
volume: 0,
position: 0,
paused: true
});
const onClick = () => {
setState({
...state,
position: 0,
paused: false
})
}
}
useContext
Using context without hooks
import { ThemeContext } from './context';
const Counter = props => {
const [count, setCount] = useState(0);
return (
<ThemeContext.Consumer>
{theme => (
<Button theme={theme} onClick={...}>
{count}
</Button>
)}
</ThemeContext.Consumer>
)
}
useContext hook
import React, { useContext } from 'react';
import { ThemeContext } from './context';
const Counter = props => {
const [count, setCount] = useState(0);
const theme = useContext(ThemeContext)
return (
<Button theme={theme} onClick={...}>
{count}
</Button>
)
}
useContext hook
import React, { useContext } from 'react';
import { ThemeContext } from './context';
const Counter = props => {
const [count, setCount] = useState(0);
const theme = useContext(ThemeContext)
return (
<Button theme={theme} onClick={...}>
{count}
</Button>
)
}
useRef
useRef
const TextInputWithFocusButton = (props) => {
const inputRef = useRef();
return (
<>
<input ref={inputRef} type="text" />
<button onClick={() => inputRef.current.focus()}>
Focus the input
</button>
</>
);
}
useRef
const TextInputWithFocusButton = (props) => {
const inputRef = useRef();
return (
<>
<input ref={inputRef} type="text" />
<button onClick={() => inputRef.current.focus()}>
Focus the input
</button>
</>
);
}
useRef
const TextInputWithFocusButton = (props) => {
const inputRef = useRef();
return (
<>
<input ref={inputRef} type="text" />
<button onClick={() => inputRef.current.focus()}>
Focus the input
</button>
</>
);
}
useRef
const TextInputWithFocusButton = (props) => {
const inputRef = useRef();
return (
<>
<input ref={inputRef} type="text" />
<button onClick={() => inputRef.current.focus()}>
Focus the input
</button>
</>
);
}
Rules of Hooks
● Only Call Hooks at the Top Level!
○ Don’t call Hooks inside loops, conditions, or nested functions
○ Order Matters!
● Only Call Hooks from React Functions
○ Or from custom hooks
Under the hood
MyComponent
Memoized State Array
Hooks
Dispatcher
MyComponent
- Initial Render
useState()
useRef()
useState()
State hook
Ref hook
State hook
Slot 0
Slot 1
Slot 2
Under the hood
MyComponent
Memoized State Array
Hooks
Dispatcher
useState()
useRef()
useState()
State hook
Ref hook
State hook
useState()
Search on Slot 0
MyComponent
- Initial Render
MyComponent
- 2nd Render
Under the hood
MyComponent
Memoized State Array
Hooks
Dispatcher
useState()
useRef()
useState()
State hook
Ref hook
State hook
useState()
useRef()
useState()
Search on Slot 1MyComponent
- 2nd Render
MyComponent
- Initial Render
useEffect
Executing something on every render using lifecycle
class CounterButton extends Component {
constructor() {
super();
this.state = {count: 0}
}
componentDidMount() {
console.log(`The count is now ${this.state.count}`)
}
componentDidUpdate() {
console.log(`The count is now ${this.state.count}`)
}
render() {
return <button onClick={() => this.setState(prevState => ({ count: prevState.count + 1 }))}>
{ this.state.count }
</button>
}
}
Executing something on every render using useEffect
const Counter = props => {
const [count, setCount] = useState(0);
useEffect(() => {
console.log(`The count is now ${count}`)
});
return <button onClick={() => setCount(count + 1)}>
{ count }
</button>
}
Executing something on every render using useEffect
const Counter = props => {
const [count, setCount] = useState(0);
useEffect(() => {
console.log(`The count is now ${count}`)
});
return <button onClick ={() => setCount(count + 1)}>
{ count }
</button>
}
Effects with Cleanup
useEffect(() => {
console.log(`The count is now ${count}`);
return function cleanup() {
console.log('cleaning up');
}
});
Effects with Cleanup
useEffect(() => {
console.log(`The count is now ${count}`);
return function cleanup() {
console.log('cleaning up');
}
});
Should my effect run on
every render?
Consider the next scenario...
//timer changes every 100ms
const Counter = ({timer}) => {
const [count, setCount] = useState(0);
useEffect(() => {
console.log(`The count is now ${count}`)
});
return <MyComponent onClick={() => setCount(count + 1)} timer={timer}>
{ count }
</MyComponent>
}
useEffect 2nd parameter
useEffect(() => {
console.log(`The count is now ${count}`)
}, [count]);
componentWillReceiveProps
componentWillReceiveProps(nextProps) {
if (this.props.timer !== nextProps.timer) {
console.log(`The timer is now ${nextProps.timer}`)
}
}
componentWillReceiveProps - hooks version
useEffect(() => {
console.log(`Timer is now ${props.timer}`);
}, [props.timer]);
componentDidMount - hooks version
useEffect(() => {
console.log(`I just mounted!`)
}, []);
componentWillUnmount - hooks version
useEffect(() => {
return function cleanup(){
console.log(`I'm unmounting!`)
}
}, []);
Let’s combine what
we learned so far
class ChatPage extends Component {
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage = this.onNewMessage. bind(this);
}
componentDidMount () {
SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage );
}
componentWillUnmount () {
SocketClient. unsubscribe (this.props.roomId);
}
componentWillReceiveProps (nextProps ) {
if (nextProps .roomId !== this.props.roomId) {
this.setState ({ messages: [] });
SocketClient. unsubscribe (this.props.roomId);
SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage );
}
}
onNewMessage (message) {
this.setState ({ messages: [...this.state.messages , message] })
}
render() {
return this.state.messages. map((text, i) => <div key={i}>{text}</div>)
}
}
class ChatPage extends Component {
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage = this.onNewMessage. bind(this);
}
componentDidMount () {
SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage );
}
componentWillUnmount () {
SocketClient. unsubscribe (this.props.roomId);
}
componentWillReceiveProps (nextProps ) {
if (nextProps .roomId !== this.props.roomId) {
this.setState ({ messages: [] });
SocketClient. unsubscribe (this.props.roomId);
SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage );
}
}
onNewMessage (message) {
this.setState ({ messages: [...this.state.messages , message] })
}
render() {
return this.state.messages. map((text, i) => <div key={i}>{text}</div>)
}
}
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage =
this.onNewMessage.bind(this);
}
class ChatPage extends Component {
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage = this.onNewMessage. bind(this);
}
componentDidMount () {
SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage );
}
componentWillUnmount () {
SocketClient. unsubscribe (this.props.roomId);
}
componentWillReceiveProps (nextProps ) {
if (nextProps .roomId !== this.props.roomId) {
this.setState ({ messages: [] });
SocketClient. unsubscribe (this.props.roomId);
SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage );
}
}
onNewMessage (message) {
this.setState ({ messages: [...this.state.messages , message] })
}
render() {
return this.state.messages. map((text, i) => <div key={i}>{text}</div>)
}
}
onNewMessage(message) {
this.setState({
messages: [...this.state.messages, message]
})
}
class ChatPage extends Component {
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage = this.onNewMessage. bind(this);
}
componentDidMount () {
SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage );
}
componentWillUnmount () {
SocketClient. unsubscribe (this.props.roomId);
}
componentWillReceiveProps (nextProps ) {
if (nextProps .roomId !== this.props.roomId) {
this.setState ({ messages: [] });
SocketClient. unsubscribe (this.props.roomId);
SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage );
}
}
onNewMessage (message) {
this.setState ({ messages: [...this.state.messages , message] })
}
render() {
return this.state.messages. map((text, i) => <div key={i}>{text}</div>)
}
}
componentDidMount () {
SocketClient.subscribeForNewMessages (
this.props.roomId,
this.onNewMessage
);
}
class ChatPage extends Component {
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage = this.onNewMessage. bind(this);
}
componentDidMount () {
SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage );
}
componentWillUnmount () {
SocketClient. unsubscribe (this.props.roomId);
}
componentWillReceiveProps (nextProps ) {
if (nextProps .roomId !== this.props.roomId) {
this.setState ({ messages: [] });
SocketClient. unsubscribe (this.props.roomId);
SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage );
}
}
onNewMessage (message) {
this.setState ({ messages: [...this.state.messages , message] })
}
render() {
return this.state.messages. map((text, i) => <div key={i}>{text}</div>)
}
}
componentWillUnmount () {
SocketClient.unsubscribe(
this.props.roomId
);
}
class ChatPage extends Component {
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage = this.onNewMessage. bind(this);
}
componentDidMount () {
SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage );
}
componentWillUnmount () {
SocketClient. unsubscribe (this.props.roomId);
}
componentWillReceiveProps (nextProps ) {
if (nextProps .roomId !== this.props.roomId) {
this.setState ({ messages: [] });
SocketClient. unsubscribe (this.props.roomId);
SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage );
}
}
onNewMessage (message) {
this.setState ({ messages: [...this.state.messages , message] })
}
render() {
return this.state.messages. map((text, i) => <div key={i}>{text}</div>)
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.roomId !== this.props.roomId) {
this.setState({ messages: [] });
SocketClient.unsubscribe(this.props.roomId);
SocketClient.subscribeForNewMessages(
nextProps.roomId,
this.onNewMessage
);
}
}
class ChatPage extends Component {
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage = this.onNewMessage. bind(this);
}
componentDidMount () {
SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage );
}
componentWillUnmount () {
SocketClient. unsubscribe (this.props.roomId);
}
componentWillReceiveProps (nextProps ) {
if (nextProps .roomId !== this.props.roomId) {
this.setState ({ messages: [] });
SocketClient. unsubscribe (this.props.roomId);
SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage );
}
}
onNewMessage (message) {
this.setState ({ messages: [...this.state.messages , message] })
}
render() {
return this.state.messages. map((text, i) => <div key={i}>{text}</div>)
}
}
render() {
return this.state.messages
.map((text, i) =>
<div key={i}>{text}</div>
)
}
const ChatPage = ({ roomId }) => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages([]);
const onNewMessage = (message) => setMessages([...messages, message]);
SocketClient.subscribeForNewMessages (roomId, onNewMessage);
return () => SocketClient.unsubscribe(roomId);
}, [roomId]);
return messages.map((text, i) => <div key={i}>{text}</div>)
}
const ChatPage = ({ roomId }) => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages([]);
const onNewMessage = (message) => setMessages([ ...messages, message]);
SocketClient.subscribeForNewMessages(roomId, onNewMessage);
return () => SocketClient.unsubscribe(roomId);
}, [roomId]);
return messages.map((text, i) => <div key={i}>{text}</div>)
}
const ChatPage = ({ roomId }) => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages([]);
const onNewMessage = (message) => setMessages([ ...messages, message]);
SocketClient.subscribeForNewMessages(roomId, onNewMessage);
return () => SocketClient.unsubscribe(roomId);
}, [roomId]);
return messages.map((text, i) => <div key={i}>{text}</div>)
}
const ChatPage = ({ roomId }) => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages([]);
const onNewMessage = (message) => setMessages([ ...messages, message]);
SocketClient.subscribeForNewMessages(roomId, onNewMessage);
return () => SocketClient.unsubscribe(roomId);
}, [roomId]);
return messages.map((text, i) => <div key={i}>{text}</div>)
}
const ChatPage = ({ roomId }) => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages([]);
const onNewMessage = (message) => setMessages([...messages, message]);
SocketClient.subscribeForNewMessages(roomId, onNewMessage);
return () => SocketClient.unsubscribe(roomId);
}, [roomId]);
return messages.map((text, i) => <div key={i}>{text}</div>)
}
const ChatPage = ({ roomId }) => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages([]);
const onNewMessage = (message) => setMessages([ ...messages, message]);
SocketClient.subscribeForNewMessages (roomId, onNewMessage);
return () => SocketClient.unsubscribe(roomId);
}, [roomId]);
return messages.map((text, i) => <div key={i}>{text}</div>)
}
const ChatPage = ({ roomId }) => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages([]);
const onNewMessage = (message) => setMessages([ ...messages, message]);
SocketClient.subscribeForNewMessages(roomId, onNewMessage);
return () => SocketClient.unsubscribe(roomId);
}, [roomId]);
return messages.map((text, i) => <div key={i}>{text}</div>)
}
Custom Hooks
What are Custom Hooks?
● Basically functions that run hooks
● Like any other function - they can can take any args and return
whatever you want
● By convention - custom hook name start with “use”
● Like any other hook - must be called on the top level of our
components
This is the custom hook:
const useChatMessages = (roomId) => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages([]);
const onNewMessage = (message) => setMessages([...messages, message]);
SocketClient.subscribeForNewMessages(roomId, onNewMessage);
return () => SocketClient.unsubscribe(roomId);
}, [roomId]);
return messages;
}
This is the (very short) component:
const ChatPage = ({ roomId }) => {
const messages = useChatMessages(roomId);
return messages.map((text, i) =>
<div key={i}>{text}</div>)
}
class ChatPage extends Component {
constructor() {
super();
this.state = {
messages: []
}
this.onNewMessage = this.onNewMessage. bind(this);
}
componentDidMount () {
SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage );
}
componentWillUnmount () {
SocketClient. unsubscribe (this.props.roomId);
}
componentWillReceiveProps (nextProps ) {
if (nextProps .roomId !== this.props.roomId) {
this.setState ({ messages: [] });
SocketClient. unsubscribe (this.props.roomId);
SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage );
}
}
onNewMessage (message) {
this.setState ({ messages: [...this.state.messages , message] })
}
render() {
return this.state.messages. map((text, i) => <div key={i}>{text}</div>)
}
}
Remember how it used to look?28 lines vs. 5 with custom hook
Custom Hooks allow us to..
● Easily share code
● Keep our components clean and readable
● Use Hooks from npm packages
Other hooks
● useReducer
● useCallback
● useMemo
● useImperativeMethods
● useLayoutEffect
Memo
React.memo
PureComponent for function components
import React, {memo} from 'react';
const MyComponent = props => { ... }
export default memo(MyComponent);
Introduced on React 16.6
React.memo
PureComponent for function components
import React, {memo} from 'react';
const MyComponent = props => { ... }
const areEqual = (prevProps, nextProps) => { ... }
export default memo(MyComponent, areEqual);
Introduced on React 16.6
<MyComponent x={1} y={2}/>
DOM
render
x=1
y=2
Memo
Cache
<MyComponent x={2} y={2}/>
render
x=2
y=2
<MyComponent x={2} y={3}/>
1
2
3
Rendering “MyComponent” with Memo
x=1
y=2
x=2
y=2
areEqual = (prev, next) => prev.x === next.x
Suspense + Lazy
Lazy
Code-Splitting
import React, { lazy } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent' ));
const MyComponent = (props) =>
<div>
<OtherComponent/>
</div>
Introduced on React 16.6
Lazy
Code-Splitting
import React, { lazy } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const MyComponent = (props) =>
<div>
<OtherComponent/>
</div>
Introduced on React 16.6
Lazy
Code-Splitting
import React, { lazy } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent' ));
const MyComponent = (props) =>
<div>
<OtherComponent/>
</div>
Introduced on React 16.6
Lazy
Code-Splitting
import React, { lazy } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const MyComponent = (props) =>
<div>
<OtherComponent/>
</div>
Introduced on React 16.6
Lazy + Suspense
Code-Splitting
import React, { lazy, Suspense } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent' ));
const MyComponent = (props) =>
<div>
<Suspense fallback={<div>Loading..</div>}>
<OtherComponent />
</Suspense>
</div>
Introduced on React 16.6
Lazy + Suspense
Code-Splitting
import React, { lazy, Suspense } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const MyComponent = (props) =>
<div>
<Suspense fallback ={<div>Loading..</div>}>
<OtherComponent />
</Suspense>
</div>
Introduced on React 16.6
Lazy + Suspense
Code-Splitting
import React, { lazy, Suspense } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const MyComponent = (props) =>
<div>
<Suspense fallback={<div>Loading..</div>}>
<OtherComponent />
</Suspense>
</div>
Introduced on React 16.6
const Home = lazy(() => import('./components/Home ));
const Posts = lazy(() => import('./components/Posts' ));
const App = () => (
<Router>
<Suspense fallback={<Loading />}>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/posts" component={Posts} />
</Switch>
</Suspense>
</Router>
)
Hook me up with
Questions
nirsky
Nir_Hadassi
THANKS!
nirsky
Nir_Hadassi

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdf
 
React Context API
React Context APIReact Context API
React Context API
 
React js
React jsReact js
React js
 
React web development
React web developmentReact web development
React web development
 
React workshop
React workshopReact workshop
React workshop
 
React Hooks
React HooksReact Hooks
React Hooks
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
Introduction to react_js
Introduction to react_jsIntroduction to react_js
Introduction to react_js
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
React and redux
React and reduxReact and redux
React and redux
 
Intro to React
Intro to ReactIntro to React
Intro to React
 

Ähnlich wie React new features and intro to Hooks

Recompacting your react application
Recompacting your react applicationRecompacting your react application
Recompacting your react applicationGreg Bergé
 
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JSFestUA
 
N Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React NativeN Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React NativeAnton Kulyk
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projectsIgnacio Martín
 
How do we use hooks
How do we use hooksHow do we use hooks
How do we use hooksJim Liu
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsEvangelia Mitsopoulou
 
Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019
Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019
Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019Codemotion
 
React JS Hooks Sheet .pdf
React JS Hooks Sheet .pdfReact JS Hooks Sheet .pdf
React JS Hooks Sheet .pdfnishant078cs23
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfindiaartz
 
Higher Order Components and Render Props
Higher Order Components and Render PropsHigher Order Components and Render Props
Higher Order Components and Render PropsNitish Phanse
 
react-hooks.pdf
react-hooks.pdfreact-hooks.pdf
react-hooks.pdfchengbo xu
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionVisual Engineering
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
React Back to the Future
React Back to the FutureReact Back to the Future
React Back to the Future500Tech
 
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)indeedeng
 
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...Edureka!
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practicesClickky
 

Ähnlich wie React new features and intro to Hooks (20)

Road to react hooks
Road to react hooksRoad to react hooks
Road to react hooks
 
Recompacting your react application
Recompacting your react applicationRecompacting your react application
Recompacting your react application
 
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
 
N Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React NativeN Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React Native
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projects
 
How do we use hooks
How do we use hooksHow do we use hooks
How do we use hooks
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applications
 
Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019
Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019
Matteo Antony Mistretta - Refactoring into React hooks - Codemotion Rome 2019
 
React JS Hooks Sheet .pdf
React JS Hooks Sheet .pdfReact JS Hooks Sheet .pdf
React JS Hooks Sheet .pdf
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
 
Higher Order Components and Render Props
Higher Order Components and Render PropsHigher Order Components and Render Props
Higher Order Components and Render Props
 
react-hooks.pdf
react-hooks.pdfreact-hooks.pdf
react-hooks.pdf
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
React Back to the Future
React Back to the FutureReact Back to the Future
React Back to the Future
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
 
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
 
JavaScript Refactoring
JavaScript RefactoringJavaScript Refactoring
JavaScript Refactoring
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 

Mehr von Soluto

Solving trust issues at scale - AppSec California
Solving trust issues at scale - AppSec CaliforniaSolving trust issues at scale - AppSec California
Solving trust issues at scale - AppSec CaliforniaSoluto
 
Solving trust issues at scale
Solving trust issues at scaleSolving trust issues at scale
Solving trust issues at scaleSoluto
 
Things I wish someone had told me about Istio, Omer Levi Hevroni
Things I wish someone had told me about Istio, Omer Levi HevroniThings I wish someone had told me about Istio, Omer Levi Hevroni
Things I wish someone had told me about Istio, Omer Levi HevroniSoluto
 
Can Kubernetes Keep a Secret? - Women in AppSec Webinar
Can Kubernetes Keep a Secret? - Women in AppSec WebinarCan Kubernetes Keep a Secret? - Women in AppSec Webinar
Can Kubernetes Keep a Secret? - Women in AppSec WebinarSoluto
 
FTRD - Can Kubernetes Keep a Secret?
FTRD -  Can Kubernetes Keep a Secret?FTRD -  Can Kubernetes Keep a Secret?
FTRD - Can Kubernetes Keep a Secret?Soluto
 
The Dark Side of Monitoring
The Dark Side of MonitoringThe Dark Side of Monitoring
The Dark Side of MonitoringSoluto
 
Hacking like a FED
Hacking like a FEDHacking like a FED
Hacking like a FEDSoluto
 
Monitoria@Icinga camp berlin
Monitoria@Icinga camp berlinMonitoria@Icinga camp berlin
Monitoria@Icinga camp berlinSoluto
 
Can Kubernetes Keep a Secret?
Can Kubernetes Keep a Secret?Can Kubernetes Keep a Secret?
Can Kubernetes Keep a Secret?Soluto
 
Kamus intro
Kamus introKamus intro
Kamus introSoluto
 
Secure Your Pipeline
Secure Your PipelineSecure Your Pipeline
Secure Your PipelineSoluto
 
Secure the Pipeline - OWASP Poland Day 2018
Secure the Pipeline - OWASP Poland Day 2018Secure the Pipeline - OWASP Poland Day 2018
Secure the Pipeline - OWASP Poland Day 2018Soluto
 
Monitoria@reversim
Monitoria@reversimMonitoria@reversim
Monitoria@reversimSoluto
 
Languages don't matter anymore!
Languages don't matter anymore!Languages don't matter anymore!
Languages don't matter anymore!Soluto
 
Security Testing for Containerized Applications
Security Testing for Containerized ApplicationsSecurity Testing for Containerized Applications
Security Testing for Containerized ApplicationsSoluto
 
Owasp glue
Owasp glueOwasp glue
Owasp glueSoluto
 
Unify logz with fluentd
Unify logz with fluentdUnify logz with fluentd
Unify logz with fluentdSoluto
 
Storing data in Redis like a pro
Storing data in Redis like a proStoring data in Redis like a pro
Storing data in Redis like a proSoluto
 
Monitor all the thingz slideshare
Monitor all the thingz slideshareMonitor all the thingz slideshare
Monitor all the thingz slideshareSoluto
 
Authentication without Authentication - AppSec California
Authentication without Authentication - AppSec CaliforniaAuthentication without Authentication - AppSec California
Authentication without Authentication - AppSec CaliforniaSoluto
 

Mehr von Soluto (20)

Solving trust issues at scale - AppSec California
Solving trust issues at scale - AppSec CaliforniaSolving trust issues at scale - AppSec California
Solving trust issues at scale - AppSec California
 
Solving trust issues at scale
Solving trust issues at scaleSolving trust issues at scale
Solving trust issues at scale
 
Things I wish someone had told me about Istio, Omer Levi Hevroni
Things I wish someone had told me about Istio, Omer Levi HevroniThings I wish someone had told me about Istio, Omer Levi Hevroni
Things I wish someone had told me about Istio, Omer Levi Hevroni
 
Can Kubernetes Keep a Secret? - Women in AppSec Webinar
Can Kubernetes Keep a Secret? - Women in AppSec WebinarCan Kubernetes Keep a Secret? - Women in AppSec Webinar
Can Kubernetes Keep a Secret? - Women in AppSec Webinar
 
FTRD - Can Kubernetes Keep a Secret?
FTRD -  Can Kubernetes Keep a Secret?FTRD -  Can Kubernetes Keep a Secret?
FTRD - Can Kubernetes Keep a Secret?
 
The Dark Side of Monitoring
The Dark Side of MonitoringThe Dark Side of Monitoring
The Dark Side of Monitoring
 
Hacking like a FED
Hacking like a FEDHacking like a FED
Hacking like a FED
 
Monitoria@Icinga camp berlin
Monitoria@Icinga camp berlinMonitoria@Icinga camp berlin
Monitoria@Icinga camp berlin
 
Can Kubernetes Keep a Secret?
Can Kubernetes Keep a Secret?Can Kubernetes Keep a Secret?
Can Kubernetes Keep a Secret?
 
Kamus intro
Kamus introKamus intro
Kamus intro
 
Secure Your Pipeline
Secure Your PipelineSecure Your Pipeline
Secure Your Pipeline
 
Secure the Pipeline - OWASP Poland Day 2018
Secure the Pipeline - OWASP Poland Day 2018Secure the Pipeline - OWASP Poland Day 2018
Secure the Pipeline - OWASP Poland Day 2018
 
Monitoria@reversim
Monitoria@reversimMonitoria@reversim
Monitoria@reversim
 
Languages don't matter anymore!
Languages don't matter anymore!Languages don't matter anymore!
Languages don't matter anymore!
 
Security Testing for Containerized Applications
Security Testing for Containerized ApplicationsSecurity Testing for Containerized Applications
Security Testing for Containerized Applications
 
Owasp glue
Owasp glueOwasp glue
Owasp glue
 
Unify logz with fluentd
Unify logz with fluentdUnify logz with fluentd
Unify logz with fluentd
 
Storing data in Redis like a pro
Storing data in Redis like a proStoring data in Redis like a pro
Storing data in Redis like a pro
 
Monitor all the thingz slideshare
Monitor all the thingz slideshareMonitor all the thingz slideshare
Monitor all the thingz slideshare
 
Authentication without Authentication - AppSec California
Authentication without Authentication - AppSec CaliforniaAuthentication without Authentication - AppSec California
Authentication without Authentication - AppSec California
 

Kürzlich hochgeladen

Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxNadaHaitham1
 

Kürzlich hochgeladen (20)

Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 

React new features and intro to Hooks

  • 1. AND INTRO TO HOOKS Nir Hadassi Software Engineer @ Soluto REACT NEW FEATURES
  • 2. This talk was supposed to be about... High-Order-Components and Recompose
  • 5. @TheRealMohaz About Myself Nir Hadassi 4years working at Soluto 3years working with React nirsky
  • 6. What are React Hooks? “Hooks lets you use state and other React features without writing a class.” Introduced on React v16.7.0-alpha
  • 8. Why classes are bad? ● Complex components become hard to understand ● Classes confuse people (notorious this..) ● Classes confuse machines (don’t minify well) ● It’s hard to reuse stateful logic between classes
  • 9. Agenda 1. Hooks Intro a. useState b. useRef c. useContext d. useEffect 2. Memo 3. Lazy
  • 11.
  • 12. Class with state class CounterButton extends Component { constructor() { super(); this.state = { count: 0 } } render() { return <button onClick={() => this.setState(prevState => ({ count: prevState.count + 1 }))}> { this.state.count } </button> } }
  • 13. With useState import React, { useState } from 'react'; const Counter = props => { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}> { count } </button> }
  • 14. With useState import React, { useState } from 'react'; const Counter = props => { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}> { count } </button> }
  • 15. With useState import React, { useState } from 'react'; const Counter = props => { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}> { count } </button> }
  • 16. With useState import React, { useState } from 'react'; const Counter = props => { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}> { count } </button> }
  • 17. Multiple State Variables const Player = props => { const [volume, setVolume] = useState(0); const [position, setPosition] = useState(0); const [paused, setPaused] = useState(true); const onClick = () => { setPosition(0); setPaused(false); } }
  • 18. Multiple State Variables const Player = props => { const [state, setState] = useState({ volume: 0, position: 0, paused: true }); const onClick = () => { setState({ ...state, position: 0, paused: false }) } }
  • 20. Using context without hooks import { ThemeContext } from './context'; const Counter = props => { const [count, setCount] = useState(0); return ( <ThemeContext.Consumer> {theme => ( <Button theme={theme} onClick={...}> {count} </Button> )} </ThemeContext.Consumer> ) }
  • 21. useContext hook import React, { useContext } from 'react'; import { ThemeContext } from './context'; const Counter = props => { const [count, setCount] = useState(0); const theme = useContext(ThemeContext) return ( <Button theme={theme} onClick={...}> {count} </Button> ) }
  • 22. useContext hook import React, { useContext } from 'react'; import { ThemeContext } from './context'; const Counter = props => { const [count, setCount] = useState(0); const theme = useContext(ThemeContext) return ( <Button theme={theme} onClick={...}> {count} </Button> ) }
  • 24. useRef const TextInputWithFocusButton = (props) => { const inputRef = useRef(); return ( <> <input ref={inputRef} type="text" /> <button onClick={() => inputRef.current.focus()}> Focus the input </button> </> ); }
  • 25. useRef const TextInputWithFocusButton = (props) => { const inputRef = useRef(); return ( <> <input ref={inputRef} type="text" /> <button onClick={() => inputRef.current.focus()}> Focus the input </button> </> ); }
  • 26. useRef const TextInputWithFocusButton = (props) => { const inputRef = useRef(); return ( <> <input ref={inputRef} type="text" /> <button onClick={() => inputRef.current.focus()}> Focus the input </button> </> ); }
  • 27. useRef const TextInputWithFocusButton = (props) => { const inputRef = useRef(); return ( <> <input ref={inputRef} type="text" /> <button onClick={() => inputRef.current.focus()}> Focus the input </button> </> ); }
  • 28. Rules of Hooks ● Only Call Hooks at the Top Level! ○ Don’t call Hooks inside loops, conditions, or nested functions ○ Order Matters! ● Only Call Hooks from React Functions ○ Or from custom hooks
  • 29. Under the hood MyComponent Memoized State Array Hooks Dispatcher MyComponent - Initial Render useState() useRef() useState() State hook Ref hook State hook Slot 0 Slot 1 Slot 2
  • 30. Under the hood MyComponent Memoized State Array Hooks Dispatcher useState() useRef() useState() State hook Ref hook State hook useState() Search on Slot 0 MyComponent - Initial Render MyComponent - 2nd Render
  • 31. Under the hood MyComponent Memoized State Array Hooks Dispatcher useState() useRef() useState() State hook Ref hook State hook useState() useRef() useState() Search on Slot 1MyComponent - 2nd Render MyComponent - Initial Render
  • 33. Executing something on every render using lifecycle class CounterButton extends Component { constructor() { super(); this.state = {count: 0} } componentDidMount() { console.log(`The count is now ${this.state.count}`) } componentDidUpdate() { console.log(`The count is now ${this.state.count}`) } render() { return <button onClick={() => this.setState(prevState => ({ count: prevState.count + 1 }))}> { this.state.count } </button> } }
  • 34. Executing something on every render using useEffect const Counter = props => { const [count, setCount] = useState(0); useEffect(() => { console.log(`The count is now ${count}`) }); return <button onClick={() => setCount(count + 1)}> { count } </button> }
  • 35. Executing something on every render using useEffect const Counter = props => { const [count, setCount] = useState(0); useEffect(() => { console.log(`The count is now ${count}`) }); return <button onClick ={() => setCount(count + 1)}> { count } </button> }
  • 36. Effects with Cleanup useEffect(() => { console.log(`The count is now ${count}`); return function cleanup() { console.log('cleaning up'); } });
  • 37. Effects with Cleanup useEffect(() => { console.log(`The count is now ${count}`); return function cleanup() { console.log('cleaning up'); } });
  • 38. Should my effect run on every render? Consider the next scenario...
  • 39. //timer changes every 100ms const Counter = ({timer}) => { const [count, setCount] = useState(0); useEffect(() => { console.log(`The count is now ${count}`) }); return <MyComponent onClick={() => setCount(count + 1)} timer={timer}> { count } </MyComponent> }
  • 40.
  • 41. useEffect 2nd parameter useEffect(() => { console.log(`The count is now ${count}`) }, [count]);
  • 42. componentWillReceiveProps componentWillReceiveProps(nextProps) { if (this.props.timer !== nextProps.timer) { console.log(`The timer is now ${nextProps.timer}`) } }
  • 43. componentWillReceiveProps - hooks version useEffect(() => { console.log(`Timer is now ${props.timer}`); }, [props.timer]);
  • 44. componentDidMount - hooks version useEffect(() => { console.log(`I just mounted!`) }, []);
  • 45. componentWillUnmount - hooks version useEffect(() => { return function cleanup(){ console.log(`I'm unmounting!`) } }, []);
  • 46. Let’s combine what we learned so far
  • 47.
  • 48. class ChatPage extends Component { constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage. bind(this); } componentDidMount () { SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage ); } componentWillUnmount () { SocketClient. unsubscribe (this.props.roomId); } componentWillReceiveProps (nextProps ) { if (nextProps .roomId !== this.props.roomId) { this.setState ({ messages: [] }); SocketClient. unsubscribe (this.props.roomId); SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage ); } } onNewMessage (message) { this.setState ({ messages: [...this.state.messages , message] }) } render() { return this.state.messages. map((text, i) => <div key={i}>{text}</div>) } }
  • 49. class ChatPage extends Component { constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage. bind(this); } componentDidMount () { SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage ); } componentWillUnmount () { SocketClient. unsubscribe (this.props.roomId); } componentWillReceiveProps (nextProps ) { if (nextProps .roomId !== this.props.roomId) { this.setState ({ messages: [] }); SocketClient. unsubscribe (this.props.roomId); SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage ); } } onNewMessage (message) { this.setState ({ messages: [...this.state.messages , message] }) } render() { return this.state.messages. map((text, i) => <div key={i}>{text}</div>) } } constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage.bind(this); }
  • 50. class ChatPage extends Component { constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage. bind(this); } componentDidMount () { SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage ); } componentWillUnmount () { SocketClient. unsubscribe (this.props.roomId); } componentWillReceiveProps (nextProps ) { if (nextProps .roomId !== this.props.roomId) { this.setState ({ messages: [] }); SocketClient. unsubscribe (this.props.roomId); SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage ); } } onNewMessage (message) { this.setState ({ messages: [...this.state.messages , message] }) } render() { return this.state.messages. map((text, i) => <div key={i}>{text}</div>) } } onNewMessage(message) { this.setState({ messages: [...this.state.messages, message] }) }
  • 51. class ChatPage extends Component { constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage. bind(this); } componentDidMount () { SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage ); } componentWillUnmount () { SocketClient. unsubscribe (this.props.roomId); } componentWillReceiveProps (nextProps ) { if (nextProps .roomId !== this.props.roomId) { this.setState ({ messages: [] }); SocketClient. unsubscribe (this.props.roomId); SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage ); } } onNewMessage (message) { this.setState ({ messages: [...this.state.messages , message] }) } render() { return this.state.messages. map((text, i) => <div key={i}>{text}</div>) } } componentDidMount () { SocketClient.subscribeForNewMessages ( this.props.roomId, this.onNewMessage ); }
  • 52. class ChatPage extends Component { constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage. bind(this); } componentDidMount () { SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage ); } componentWillUnmount () { SocketClient. unsubscribe (this.props.roomId); } componentWillReceiveProps (nextProps ) { if (nextProps .roomId !== this.props.roomId) { this.setState ({ messages: [] }); SocketClient. unsubscribe (this.props.roomId); SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage ); } } onNewMessage (message) { this.setState ({ messages: [...this.state.messages , message] }) } render() { return this.state.messages. map((text, i) => <div key={i}>{text}</div>) } } componentWillUnmount () { SocketClient.unsubscribe( this.props.roomId ); }
  • 53. class ChatPage extends Component { constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage. bind(this); } componentDidMount () { SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage ); } componentWillUnmount () { SocketClient. unsubscribe (this.props.roomId); } componentWillReceiveProps (nextProps ) { if (nextProps .roomId !== this.props.roomId) { this.setState ({ messages: [] }); SocketClient. unsubscribe (this.props.roomId); SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage ); } } onNewMessage (message) { this.setState ({ messages: [...this.state.messages , message] }) } render() { return this.state.messages. map((text, i) => <div key={i}>{text}</div>) } } componentWillReceiveProps(nextProps) { if (nextProps.roomId !== this.props.roomId) { this.setState({ messages: [] }); SocketClient.unsubscribe(this.props.roomId); SocketClient.subscribeForNewMessages( nextProps.roomId, this.onNewMessage ); } }
  • 54. class ChatPage extends Component { constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage. bind(this); } componentDidMount () { SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage ); } componentWillUnmount () { SocketClient. unsubscribe (this.props.roomId); } componentWillReceiveProps (nextProps ) { if (nextProps .roomId !== this.props.roomId) { this.setState ({ messages: [] }); SocketClient. unsubscribe (this.props.roomId); SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage ); } } onNewMessage (message) { this.setState ({ messages: [...this.state.messages , message] }) } render() { return this.state.messages. map((text, i) => <div key={i}>{text}</div>) } } render() { return this.state.messages .map((text, i) => <div key={i}>{text}</div> ) }
  • 55. const ChatPage = ({ roomId }) => { const [messages, setMessages] = useState([]); useEffect(() => { setMessages([]); const onNewMessage = (message) => setMessages([...messages, message]); SocketClient.subscribeForNewMessages (roomId, onNewMessage); return () => SocketClient.unsubscribe(roomId); }, [roomId]); return messages.map((text, i) => <div key={i}>{text}</div>) }
  • 56. const ChatPage = ({ roomId }) => { const [messages, setMessages] = useState([]); useEffect(() => { setMessages([]); const onNewMessage = (message) => setMessages([ ...messages, message]); SocketClient.subscribeForNewMessages(roomId, onNewMessage); return () => SocketClient.unsubscribe(roomId); }, [roomId]); return messages.map((text, i) => <div key={i}>{text}</div>) }
  • 57. const ChatPage = ({ roomId }) => { const [messages, setMessages] = useState([]); useEffect(() => { setMessages([]); const onNewMessage = (message) => setMessages([ ...messages, message]); SocketClient.subscribeForNewMessages(roomId, onNewMessage); return () => SocketClient.unsubscribe(roomId); }, [roomId]); return messages.map((text, i) => <div key={i}>{text}</div>) }
  • 58. const ChatPage = ({ roomId }) => { const [messages, setMessages] = useState([]); useEffect(() => { setMessages([]); const onNewMessage = (message) => setMessages([ ...messages, message]); SocketClient.subscribeForNewMessages(roomId, onNewMessage); return () => SocketClient.unsubscribe(roomId); }, [roomId]); return messages.map((text, i) => <div key={i}>{text}</div>) }
  • 59. const ChatPage = ({ roomId }) => { const [messages, setMessages] = useState([]); useEffect(() => { setMessages([]); const onNewMessage = (message) => setMessages([...messages, message]); SocketClient.subscribeForNewMessages(roomId, onNewMessage); return () => SocketClient.unsubscribe(roomId); }, [roomId]); return messages.map((text, i) => <div key={i}>{text}</div>) }
  • 60. const ChatPage = ({ roomId }) => { const [messages, setMessages] = useState([]); useEffect(() => { setMessages([]); const onNewMessage = (message) => setMessages([ ...messages, message]); SocketClient.subscribeForNewMessages (roomId, onNewMessage); return () => SocketClient.unsubscribe(roomId); }, [roomId]); return messages.map((text, i) => <div key={i}>{text}</div>) }
  • 61. const ChatPage = ({ roomId }) => { const [messages, setMessages] = useState([]); useEffect(() => { setMessages([]); const onNewMessage = (message) => setMessages([ ...messages, message]); SocketClient.subscribeForNewMessages(roomId, onNewMessage); return () => SocketClient.unsubscribe(roomId); }, [roomId]); return messages.map((text, i) => <div key={i}>{text}</div>) }
  • 63. What are Custom Hooks? ● Basically functions that run hooks ● Like any other function - they can can take any args and return whatever you want ● By convention - custom hook name start with “use” ● Like any other hook - must be called on the top level of our components
  • 64. This is the custom hook: const useChatMessages = (roomId) => { const [messages, setMessages] = useState([]); useEffect(() => { setMessages([]); const onNewMessage = (message) => setMessages([...messages, message]); SocketClient.subscribeForNewMessages(roomId, onNewMessage); return () => SocketClient.unsubscribe(roomId); }, [roomId]); return messages; }
  • 65. This is the (very short) component: const ChatPage = ({ roomId }) => { const messages = useChatMessages(roomId); return messages.map((text, i) => <div key={i}>{text}</div>) }
  • 66. class ChatPage extends Component { constructor() { super(); this.state = { messages: [] } this.onNewMessage = this.onNewMessage. bind(this); } componentDidMount () { SocketClient. subscribeForNewMessages (this.props.roomId, this.onNewMessage ); } componentWillUnmount () { SocketClient. unsubscribe (this.props.roomId); } componentWillReceiveProps (nextProps ) { if (nextProps .roomId !== this.props.roomId) { this.setState ({ messages: [] }); SocketClient. unsubscribe (this.props.roomId); SocketClient. subscribeForNewMessages (nextProps .roomId, this.onNewMessage ); } } onNewMessage (message) { this.setState ({ messages: [...this.state.messages , message] }) } render() { return this.state.messages. map((text, i) => <div key={i}>{text}</div>) } } Remember how it used to look?28 lines vs. 5 with custom hook
  • 67. Custom Hooks allow us to.. ● Easily share code ● Keep our components clean and readable ● Use Hooks from npm packages
  • 68.
  • 69. Other hooks ● useReducer ● useCallback ● useMemo ● useImperativeMethods ● useLayoutEffect
  • 70. Memo
  • 71. React.memo PureComponent for function components import React, {memo} from 'react'; const MyComponent = props => { ... } export default memo(MyComponent); Introduced on React 16.6
  • 72. React.memo PureComponent for function components import React, {memo} from 'react'; const MyComponent = props => { ... } const areEqual = (prevProps, nextProps) => { ... } export default memo(MyComponent, areEqual); Introduced on React 16.6
  • 73. <MyComponent x={1} y={2}/> DOM render x=1 y=2 Memo Cache <MyComponent x={2} y={2}/> render x=2 y=2 <MyComponent x={2} y={3}/> 1 2 3 Rendering “MyComponent” with Memo x=1 y=2 x=2 y=2 areEqual = (prev, next) => prev.x === next.x
  • 75. Lazy Code-Splitting import React, { lazy } from 'react'; const OtherComponent = React.lazy(() => import('./OtherComponent' )); const MyComponent = (props) => <div> <OtherComponent/> </div> Introduced on React 16.6
  • 76. Lazy Code-Splitting import React, { lazy } from 'react'; const OtherComponent = React.lazy(() => import('./OtherComponent')); const MyComponent = (props) => <div> <OtherComponent/> </div> Introduced on React 16.6
  • 77. Lazy Code-Splitting import React, { lazy } from 'react'; const OtherComponent = React.lazy(() => import('./OtherComponent' )); const MyComponent = (props) => <div> <OtherComponent/> </div> Introduced on React 16.6
  • 78. Lazy Code-Splitting import React, { lazy } from 'react'; const OtherComponent = React.lazy(() => import('./OtherComponent')); const MyComponent = (props) => <div> <OtherComponent/> </div> Introduced on React 16.6
  • 79. Lazy + Suspense Code-Splitting import React, { lazy, Suspense } from 'react'; const OtherComponent = React.lazy(() => import('./OtherComponent' )); const MyComponent = (props) => <div> <Suspense fallback={<div>Loading..</div>}> <OtherComponent /> </Suspense> </div> Introduced on React 16.6
  • 80. Lazy + Suspense Code-Splitting import React, { lazy, Suspense } from 'react'; const OtherComponent = React.lazy(() => import('./OtherComponent')); const MyComponent = (props) => <div> <Suspense fallback ={<div>Loading..</div>}> <OtherComponent /> </Suspense> </div> Introduced on React 16.6
  • 81. Lazy + Suspense Code-Splitting import React, { lazy, Suspense } from 'react'; const OtherComponent = React.lazy(() => import('./OtherComponent')); const MyComponent = (props) => <div> <Suspense fallback={<div>Loading..</div>}> <OtherComponent /> </Suspense> </div> Introduced on React 16.6
  • 82. const Home = lazy(() => import('./components/Home )); const Posts = lazy(() => import('./components/Posts' )); const App = () => ( <Router> <Suspense fallback={<Loading />}> <Switch> <Route exact path="/" component={Home} /> <Route path="/posts" component={Posts} /> </Switch> </Suspense> </Router> )
  • 83.
  • 84. Hook me up with Questions nirsky Nir_Hadassi