From 04d002dfd6cccbc4df514359c152e3f61bce52d4 Mon Sep 17 00:00:00 2001 From: Nivaldo Farias Date: Mon, 10 Feb 2025 23:06:44 -0300 Subject: [PATCH] Translate `rules-of-hooks.md` to pt-br (#931) --- src/content/reference/rules/rules-of-hooks.md | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/src/content/reference/rules/rules-of-hooks.md b/src/content/reference/rules/rules-of-hooks.md index ecaef7c60..351f12347 100644 --- a/src/content/reference/rules/rules-of-hooks.md +++ b/src/content/reference/rules/rules-of-hooks.md @@ -1,53 +1,53 @@ --- -title: Rules of Hooks +title: Regras dos Hooks --- -Hooks are defined using JavaScript functions, but they represent a special type of reusable UI logic with restrictions on where they can be called. +Os Hooks são definidos usando funções JavaScript, mas representam um tipo especial de lógica de UI reutilizável com restrições sobre onde podem ser chamados. --- -## Only call Hooks at the top level {/*only-call-hooks-at-the-top-level*/} +## Chame Hooks apenas no nível superior {/*only-call-hooks-at-the-top-level*/} -Functions whose names start with `use` are called [*Hooks*](/reference/react) in React. +Funções cujos nomes começam com `use` são chamadas de [*Hooks*](/reference/react) no React. -**Don’t call Hooks inside loops, conditions, nested functions, or `try`/`catch`/`finally` blocks.** Instead, always use Hooks at the top level of your React function, before any early returns. You can only call Hooks while React is rendering a function component: +**Não chame Hooks dentro de loops, condições, funções aninhadas ou blocos `try`/`catch`/`finally`.** Em vez disso, sempre use Hooks no nível superior da sua função React, antes de quaisquer retornos antecipados. Você só pode chamar Hooks enquanto o React estiver renderizando um componente de função: -* ✅ Call them at the top level in the body of a [function component](/learn/your-first-component). -* ✅ Call them at the top level in the body of a [custom Hook](/learn/reusing-logic-with-custom-hooks). +* ✅ Chame-os no nível superior no corpo de um [componente de função](/learn/your-first-component). +* ✅ Chame-os no nível superior no corpo de um [Hook personalizado](/learn/reusing-logic-with-custom-hooks). ```js{2-3,8-9} function Counter() { - // ✅ Good: top-level in a function component + // ✅ Bom: nível superior em um componente de função const [count, setCount] = useState(0); // ... } function useWindowWidth() { - // ✅ Good: top-level in a custom Hook + // ✅ Bom: nível superior em um Hook personalizado const [width, setWidth] = useState(window.innerWidth); // ... } ``` -It’s **not** supported to call Hooks (functions starting with `use`) in any other cases, for example: +Não é **suportado** chamar Hooks (funções que começam com `use`) em quaisquer outros casos, por exemplo: -* 🔴 Do not call Hooks inside conditions or loops. -* 🔴 Do not call Hooks after a conditional `return` statement. -* 🔴 Do not call Hooks in event handlers. -* 🔴 Do not call Hooks in class components. -* 🔴 Do not call Hooks inside functions passed to `useMemo`, `useReducer`, or `useEffect`. -* 🔴 Do not call Hooks inside `try`/`catch`/`finally` blocks. +* 🔴 Não chame Hooks dentro de condições ou loops. +* 🔴 Não chame Hooks após uma declaração de `return` condicional. +* 🔴 Não chame Hooks em manipuladores de eventos. +* 🔴 Não chame Hooks em componentes de classe. +* 🔴 Não chame Hooks dentro de funções passadas para `useMemo`, `useReducer` ou `useEffect`. +* 🔴 Não chame Hooks dentro de blocos `try`/`catch`/`finally`. -If you break these rules, you might see this error. +Se você quebrar essas regras, pode ver esse erro. ```js{3-4,11-12,20-21} function Bad({ cond }) { if (cond) { - // 🔴 Bad: inside a condition (to fix, move it outside!) + // 🔴 Ruim: dentro de uma condição (para corrigir, mova para fora!) const theme = useContext(ThemeContext); } // ... @@ -55,7 +55,7 @@ function Bad({ cond }) { function Bad() { for (let i = 0; i < 10; i++) { - // 🔴 Bad: inside a loop (to fix, move it outside!) + // 🔴 Ruim: dentro de um loop (para corrigir, mova para fora!) const theme = useContext(ThemeContext); } // ... @@ -65,14 +65,14 @@ function Bad({ cond }) { if (cond) { return; } - // 🔴 Bad: after a conditional return (to fix, move it before the return!) + // 🔴 Ruim: após um return condicional (para corrigir, mova antes do return!) const theme = useContext(ThemeContext); // ... } function Bad() { function handleClick() { - // 🔴 Bad: inside an event handler (to fix, move it outside!) + // 🔴 Ruim: dentro de um manipulador de evento (para corrigir, mova para fora!) const theme = useContext(ThemeContext); } // ... @@ -80,7 +80,7 @@ function Bad() { function Bad() { const style = useMemo(() => { - // 🔴 Bad: inside useMemo (to fix, move it outside!) + // 🔴 Ruim: dentro de useMemo (para corrigir, mova para fora!) const theme = useContext(ThemeContext); return createStyle(theme); }); @@ -89,7 +89,7 @@ function Bad() { class Bad extends React.Component { render() { - // 🔴 Bad: inside a class component (to fix, write a function component instead of a class!) + // 🔴 Ruim: dentro de um componente de classe (para corrigir, escreva um componente de função em vez de uma classe!) useEffect(() => {}) // ... } @@ -97,7 +97,7 @@ class Bad extends React.Component { function Bad() { try { - // 🔴 Bad: inside try/catch/finally block (to fix, move it outside!) + // 🔴 Ruim: dentro de um bloco try/catch/finally (para corrigir, mova para fora!) const [x, setX] = useState(0); } catch { const [x, setX] = useState(1); @@ -105,31 +105,31 @@ function Bad() { } ``` -You can use the [`eslint-plugin-react-hooks` plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to catch these mistakes. +Você pode usar o [`eslint-plugin-react-hooks` plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) para capturar esses erros. -[Custom Hooks](/learn/reusing-logic-with-custom-hooks) *may* call other Hooks (that's their whole purpose). This works because custom Hooks are also supposed to only be called while a function component is rendering. +[Hooks personalizados](/learn/reusing-logic-with-custom-hooks) *podem* chamar outros Hooks (esse é todo o seu propósito). Isso funciona porque os Hooks personalizados também devem ser chamados apenas enquanto um componente de função está sendo renderizado. --- -## Only call Hooks from React functions {/*only-call-hooks-from-react-functions*/} +## Chame Hooks apenas de funções React {/*only-call-hooks-from-react-functions*/} -Don’t call Hooks from regular JavaScript functions. Instead, you can: +Não chame Hooks de funções JavaScript regulares. Em vez disso, você pode: -✅ Call Hooks from React function components. -✅ Call Hooks from [custom Hooks](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component). +✅ Chamar Hooks de componentes de função React. +✅ Chamar Hooks de [Hooks personalizados](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component). -By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code. +Seguindo essa regra, você garante que toda a lógica com estado em um componente seja claramente visível a partir do seu código fonte. ```js {2,5} function FriendList() { const [onlineStatus, setOnlineStatus] = useOnlineStatus(); // ✅ } -function setOnlineStatus() { // ❌ Not a component or custom Hook! +function setOnlineStatus() { // ❌ Não é um componente ou Hook personalizado! const [onlineStatus, setOnlineStatus] = useOnlineStatus(); } -``` +``` \ No newline at end of file