|
| 1 | +--- |
| 2 | +title: Componentes Funcionais |
| 3 | +description: Componentes Funcionais são simples funções puras que podem modificar o contexto e não tem estado próprio |
| 4 | +--- |
| 5 | + |
| 6 | +Desde a v0.9.21, o Nullstack tem a simplicidade de componentes funcionais puros pronta para uso. |
| 7 | + |
| 8 | +Este segue as especificações de [componentes renderizáveis](/pt-br/componentes-renderizaveis), mas com mais ênfase no **renderizável**. |
| 9 | + |
| 10 | +Usando funções puras, você pode escrever componentes focados na renderização da seguinte maneira: |
| 11 | + |
| 12 | +```jsx |
| 13 | +const AnonComponent = function(context) { |
| 14 | + return ( |
| 15 | + <div html={context.html} id={context.id}></div> |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +function NamedComponent(context) { |
| 20 | + return ( |
| 21 | + <div html={context.html} id={context.id}></div> |
| 22 | + ) |
| 23 | +} |
| 24 | + |
| 25 | +const ArrowComponent = (context) => { |
| 26 | + return ( |
| 27 | + <div html={context.html} id={context.id}></div> |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +export default { |
| 32 | + AnonComponent, |
| 33 | + NamedComponent, |
| 34 | + ArrowComponent |
| 35 | +}; |
| 36 | +``` |
| 37 | + |
| 38 | +E exportado assim, seu uso seria: |
| 39 | + |
| 40 | +```jsx |
| 41 | +import Nullstack from 'nullstack'; |
| 42 | +import Functionals from './Functionals'; |
| 43 | + |
| 44 | +class Application extends Nullstack { |
| 45 | + |
| 46 | + render() { |
| 47 | + const html = "<p>texto</p>"; |
| 48 | + return ( |
| 49 | + <main> |
| 50 | + <Functionals.AnonComponent html={html} id="1"/> |
| 51 | + <Functionals.NamedComponent html={html} id="2"/> |
| 52 | + <Functionals.ArrowComponent html={html} id="3"/> |
| 53 | + </main> |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +export default Application; |
| 60 | +``` |
| 61 | + |
| 62 | +Então, o resultado da renderização seria diretamente semelhante a: |
| 63 | + |
| 64 | +```html |
| 65 | +<div id="1"> <p>texto</p> </div> |
| 66 | +<div id="2"> <p>texto</p> </div> |
| 67 | +<div id="3"> <p>texto</p> </div> |
| 68 | +``` |
| 69 | + |
| 70 | +## Vinculando Valores do Contexto |
| 71 | + |
| 72 | +Mesmo não tendo estado próprio, os componentes funcionais do Nullstack podem vincular e modificar valores do [`context`](/pt-br/contexto). |
| 73 | + |
| 74 | +```jsx |
| 75 | +import Nullstack from 'nullstack'; |
| 76 | +import BindPureComponent from './Functionals'; |
| 77 | + |
| 78 | +class Application extends Nullstack { |
| 79 | + |
| 80 | + prepare(context) { |
| 81 | + context.count = 0; |
| 82 | + } |
| 83 | + |
| 84 | + render() { |
| 85 | + return ( |
| 86 | + <main> |
| 87 | + <BindPureComponent/> |
| 88 | + </main> |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +export default Application; |
| 95 | +``` |
| 96 | + |
| 97 | +Registrando `counter` no `context` como no componente acima, podemos ler e modificar esse dado no **BindPureComponent** da seguinte maneira: |
| 98 | + |
| 99 | +```jsx |
| 100 | +export default function BindPureComponent(context) { |
| 101 | + return ( |
| 102 | + <div> |
| 103 | + <input type="number" bind={context.count}/> |
| 104 | + <p>{context.count}</p> |
| 105 | + </div> |
| 106 | + ) |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +> 💡 Com sua versatilidade e simplicidade, os componentes funcionais são perfeitos para bibliotecas de componentes sem estado |
| 111 | +
|
| 112 | +## Próxima Etapa |
| 113 | + |
| 114 | +⚔ Aprenda sobre a [chave `data` do contexto](/pt-br/contexto-data). |
0 commit comments