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

Criando Componentes com ReactJS

Roger Albino
February 24, 2018

Criando Componentes com ReactJS

Slides do evento Criando Componentes com ReactJS. GDG Mogi Guaçu.

Roger Albino

February 24, 2018
Tweet

More Decks by Roger Albino

Other Decks in Programming

Transcript

  1. MVC

  2. import { Component } from 'react'; class HelloMessage extends Component

    { render() { return ( <div> Hello { this.props.name } </div> ); } } export default HelloMessage;
  3. import React, {Component} from 'react'; class HelloMessage extends Component {

    render() { return React.createElement( 'div', null, 'Hello ', this.props.name ); } }
  4. Html no Javascript? const Input = () => ( <input

    class="form-control" type="text"> ); XML
  5. XML no Javascript const Input = () => ( <input

    class="form-control" type="text"> );
  6. const Input = () => ( <input className="form-control" type=“text" />

    ); const Input = () => ( <input class="form-control" type="text"> ); class é uma palavra reservada
  7. const Input = () => ( <input className="form-control" type=“text" />

    ); const Input = () => ( <input class="form-control" type="text"> ); É obrigatório o fechamento das tags
  8. CSS no Javascript? const Wrapper = styled.header` color: #212121; background:

    #f9f9f9; ` const Header = ({ title }) => ( <Wrapper>{title}</Wrapper> )
  9. Funções import React, { Component } from 'react' const Hello

    = () => ( <h1>Hello React!</h1> ) export default Hello
  10. Classes import React, { Component } from 'react' class Hello

    extends Component { render() { return ( <h1>Hello React!</h1> ) } } export default Hello
  11. <UserTitle name="José da Silva" /> class UserTitle extends Component {

    render() { return ( <h1>{this.props.name}</h1> ) } } Props
  12. Props <UserTitle /> class UserTitle extends Component { static defaultProps

    = { name: 'Ninguém' } static propTypes = { name: PropTypes.string } render() { return ( <h1>{this.props.name}</h1> ) } }
  13. Props <UserTitle /> class UserTitle extends Component { static defaultProps

    = { name: 'Ninguém' } static propTypes = { name: PropTypes.string } render() { return ( <h1>{this.props.name}</h1> ) } }
  14. State class UserTitle extends Component { state = { hiddenTitle:

    true } toggleTitle = () => { this.setState((state) => ({ hiddenTitle: !state.hiddenTitle })) } render() { return ( <div> { !hiddenTitle && (<h1>{this.props.name}</h1>)} <button type="button" onClick={this.toggleTitle}> Hidden Text </button> </div> ) } }
  15. State class UserTitle extends Component { state = { hiddenTitle:

    true } toggleTitle = () => { this.setState((state) => ({ hiddenTitle: !state.hiddenTitle })) } render() { return ( <div> { !hiddenTitle && (<h1>{this.props.name}</h1>)} <button type="button" onClick={this.toggleTitle}> Hidden Text </button> </div> ) } }
  16. let users = [ { firstName: 'Marcio', lastName: 'da Silva'

    }, { firstName: ‘Olívia', lastName: 'da Silva' }, { firstName: 'Ana', lastName: 'Almeida' } ] const listUser = () => ( <ul> {users.map(({firstName, lastName}) => ( <li> Name: <strong>{firstName} {lastName}</strong> </li> ))} </ul> )
  17. let users = [ { firstName: 'Marcio', lastName: 'da Silva'

    }, { firstName: ‘Olívia', lastName: 'da Silva' }, { firstName: 'Ana', lastName: 'Almeida' } ] const listUser = () => ( <ul> {users.filter(({ lastName }) => lastName === 'da Silva') .map(({ firstName, lastName }) => ( <li> Name: <strong>{firstName} {lastName}</strong> </li> ))} </ul> )