Skip to content

fix: use const where applicable in keeping components pure examples #7819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/content/learn/keeping-components-pure.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function Cup({ guest }) {
}

export default function TeaGathering() {
let cups = [];
const cups = [];
for (let i = 1; i <= 12; i++) {
cups.push(<Cup key={i} guest={i} />);
}
Expand Down Expand Up @@ -245,7 +245,7 @@ Rendering is a *calculation*, it shouldn't try to "do" things. Can you express t

```js src/Clock.js active
export default function Clock({ time }) {
let hours = time.getHours();
const hours = time.getHours();
if (hours >= 0 && hours <= 6) {
document.getElementById('time').className = 'night';
} else {
Expand Down Expand Up @@ -307,7 +307,7 @@ You can fix this component by calculating the `className` and including it in th

```js src/Clock.js active
export default function Clock({ time }) {
let hours = time.getHours();
const hours = time.getHours();
let className;
if (hours >= 0 && hours <= 6) {
className = 'night';
Expand Down Expand Up @@ -606,14 +606,14 @@ export default function StoryTray({ stories }) {
import { useState, useEffect } from 'react';
import StoryTray from './StoryTray.js';

let initialStories = [
const initialStories = [
{id: 0, label: "Ankit's Story" },
{id: 1, label: "Taylor's Story" },
];

export default function App() {
let [stories, setStories] = useState([...initialStories])
let time = useTime();
const [stories, setStories] = useState([...initialStories])
const time = useTime();

// HACK: Prevent the memory from growing forever while you read docs.
// We're breaking our own rules here.
Expand Down Expand Up @@ -702,14 +702,14 @@ export default function StoryTray({ stories }) {
import { useState, useEffect } from 'react';
import StoryTray from './StoryTray.js';

let initialStories = [
const initialStories = [
{id: 0, label: "Ankit's Story" },
{id: 1, label: "Taylor's Story" },
];

export default function App() {
let [stories, setStories] = useState([...initialStories])
let time = useTime();
const [stories, setStories] = useState([...initialStories])
const time = useTime();

// HACK: Prevent the memory from growing forever while you read docs.
// We're breaking our own rules here.
Expand Down Expand Up @@ -770,7 +770,7 @@ Alternatively, you could create a _new_ array (by copying the existing one) befo
```js src/StoryTray.js active
export default function StoryTray({ stories }) {
// Copy the array!
let storiesToDisplay = stories.slice();
const storiesToDisplay = stories.slice();

// Does not affect the original array:
storiesToDisplay.push({
Expand All @@ -794,14 +794,14 @@ export default function StoryTray({ stories }) {
import { useState, useEffect } from 'react';
import StoryTray from './StoryTray.js';

let initialStories = [
const initialStories = [
{id: 0, label: "Ankit's Story" },
{id: 1, label: "Taylor's Story" },
];

export default function App() {
let [stories, setStories] = useState([...initialStories])
let time = useTime();
const [stories, setStories] = useState([...initialStories])
const time = useTime();

// HACK: Prevent the memory from growing forever while you read docs.
// We're breaking our own rules here.
Expand Down