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

How to React And not shoot yourself in the foot

How to React And not shoot yourself in the foot

React is awesome... But like any awesome tool, it should be used with caution. Let's explain how React works and why/how its should be used.

Pierre GOUDJO

December 30, 2021
Tweet

More Decks by Pierre GOUDJO

Other Decks in Programming

Transcript

  1. function tick() { const element = ( <div> <h1>Hello, world!</h1>

    <h2>It is {new Date().toLocaleTimeString()}.</h2> </div> ); ReactDOM.render(element, document.getElementById('root')); } setInterval(tick, 1000); Rendering an element multiple times into the DOM
  2. class HelloMessage extends React.Component { render() { return ( <div>

    Hello {this.props.name} </div> ); } } ReactDOM.render( <HelloMessage name="Taylor" />, document.getElementById('hello-example') ); A simple React component with JSX
  3. class HelloMessage extends React.Component { render() { return React.createElement( "div",

    null, "Hello ", this.props.name ); } } ReactDOM.render(React.createElement(HelloMessage, { name: "Taylor" }), document.getElementById('hello-example')); This is the real code executed by the browser
  4. class HelloMessage extends React.Component { render() { return React.createElement( "div",

    null, "Hello ", this.props.name ); } } ReactDOM.render(React.createElement(HelloMessage, { name: "Taylor" }), document.getElementById('hello-example')); Look like the component is a “function”
  5. function tick() { const element = ( <div> <h1>Hello, world!</h1>

    <h2>It is {new Date().toLocaleTimeString()}.</h2> </div> ); ReactDOM.render( element, document.getElementById('root') ); } setInterval(tick, 1000); Let’s make this in a truly reusable component
  6. function Clock(props) { return ( <div> <h1>Hello, world!</h1> <h2>It is

    {props.date.toLocaleTimeString()}.</h2> </div> ); } function tick() { ReactDOM.render( <Clock date={new Date()} />, document.getElementById('root') ); } setInterval(tick, 1000); Clock is now an isolated component but doesn’t manage the time to be displayed
  7. class Clock extends React.Component { constructor(props) { super(props); this.state =

    {date: new Date()}; } render() { return ( <div> <h1>Hello, world!</h1> <h2>It is {this.state.date.toLocaleTimeString()}.</h2> </div> ); } } State use instead of props
  8. class Clock extends React.Component { constructor(props) { super(props); this.state =

    {date: new Date()}; } componentDidMount() { } componentWillUnmount() { } render() { return ( <div> <h1>Hello, world!</h1> <h2>It is {this.state.date.toLocaleTimeString()}.</h2> </div> ); } } Lifecycle methods
  9. class Clock extends React.Component { constructor(props) { super(props); this.state =

    {date: new Date()}; } componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 ); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ date: new Date() }); } render() { return ( <div> <h1>Hello, world!</h1> <h2>It is {this.state.date.toLocaleTimeString()}.</h2> </div> ); } } Update the date at fi xed interval
  10. const Clock = (props) => { const [date, setDate] =

    useState(new Date()) useEffect( () =>{ const timerId = setInterval( () => setDate(new Date()), 1000 ); return () =>{ clearInterval(timerId) } }, ) return ( <div> <h1>Hello, world!</h1> <h2>It is {date.toLocaleTimeString()}.</h2> </div> ) } Same thing with a functional component