-
Notifications
You must be signed in to change notification settings - Fork 0
セクション8: Fetching Data & useEffect #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
b19f457
656aa4a
f2de7d5
fbf2d5d
23c5152
7a2de2a
5683bef
3cfad3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "trips": [ | ||
| { | ||
| "title": "2 Night Stay in Venice", | ||
| "price": "£195", | ||
| "id": "1", | ||
| "loc": "europe" | ||
| }, | ||
| { | ||
| "title": "3 Night Stay in Paris", | ||
| "price": "£295", | ||
| "id": "2", | ||
| "loc": "europe" | ||
| }, | ||
| { | ||
| "title": "4 Night Stay in London", | ||
| "price": "£345", | ||
| "id": "3", | ||
| "loc": "europe" | ||
| }, | ||
| { | ||
| "title": "3 Night Stay in New York", | ||
| "price": "£325", | ||
| "id": "4", | ||
| "loc": "america" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +0,0 @@ | ||
| .App { | ||
| text-align: center; | ||
| } | ||
|
|
||
| .App-logo { | ||
| height: 40vmin; | ||
| pointer-events: none; | ||
| } | ||
|
|
||
| @media (prefers-reduced-motion: no-preference) { | ||
| .App-logo { | ||
| animation: App-logo-spin infinite 20s linear; | ||
| } | ||
| } | ||
|
|
||
| .App-header { | ||
| background-color: #282c34; | ||
| min-height: 100vh; | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| font-size: calc(10px + 2vmin); | ||
| color: white; | ||
| } | ||
|
|
||
| .App-link { | ||
| color: #61dafb; | ||
| } | ||
|
|
||
| @keyframes App-logo-spin { | ||
| from { | ||
| transform: rotate(0deg); | ||
| } | ||
| to { | ||
| transform: rotate(360deg); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| .trip-list { | ||
| width: 480px; | ||
| margin: 60px auto; | ||
| } | ||
| .trip-list ul { | ||
| padding: 0; | ||
| } | ||
| .trip-list li { | ||
| border: 1px solid #e4e4e4; | ||
| box-sizing: border-box; | ||
| padding: 10px; | ||
| box-shadow: 4px 4px 6px rgba(0,0,0,0.05); | ||
| border-radius: 4px; | ||
| list-style-type: none; | ||
| margin: 20px 0; | ||
| } | ||
| .trip-list .filters { | ||
| display: flex; | ||
| gap: 10px; | ||
| } | ||
| .trip-list button { | ||
| padding: 10px; | ||
| font-size: 16px; | ||
| flex-grow: 1; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { useState, useEffect, useCallback } from "react"; | ||
| import "./TripList.css"; | ||
|
|
||
| export default function TripList() { | ||
| const [trips, setTrips] = useState([]); | ||
| const [url, setUrl] = useState("http://localhost:3000/trips"); | ||
| const fetchTrips = useCallback(async () => { | ||
| const response = await fetch(url); | ||
| const json = await response.json(); | ||
| setTrips(json); | ||
| }, [url]); | ||
| useEffect(() => { | ||
| fetchTrips(); | ||
| }, [fetchTrips]); | ||
|
Comment on lines
+12
to
+14
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useEffectの第二引数に関数を指定する場合、関数が定義されると発火する useCallbackを使用すると関数を毎回作らなくなるが、第二引数を指定するとそれが変化した場合は関数を再定義するようになる |
||
| return ( | ||
| <div className="trip-list"> | ||
| <h2>Trip list</h2> | ||
| <ul> | ||
| {trips.map((trip) => ( | ||
| <li key={trip.id}> | ||
| <h3>{trip.title}</h3> | ||
| <p>{trip.price}</p> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| <div className="filters"> | ||
| <button | ||
| onClick={() => setUrl("http://localhost:3000/trips?loc=europe")} | ||
| > | ||
| European Trips | ||
| </button> | ||
| <button onClick={() => setUrl("http://localhost:3000/trips")}> | ||
| All trips | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +0,0 @@ | ||
| body { | ||
| margin: 0; | ||
| font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', | ||
| 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', | ||
| sans-serif; | ||
| -webkit-font-smoothing: antialiased; | ||
| -moz-osx-font-smoothing: grayscale; | ||
| } | ||
|
|
||
| code { | ||
| font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', | ||
| monospace; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,11 @@ | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom/client'; | ||
| import './index.css'; | ||
| import App from './App'; | ||
| import reportWebVitals from './reportWebVitals'; | ||
| import React from "react"; | ||
| import ReactDOM from "react-dom/client"; | ||
| import "./index.css"; | ||
| import App from "./App"; | ||
|
|
||
| const root = ReactDOM.createRoot(document.getElementById('root')); | ||
| const root = ReactDOM.createRoot(document.getElementById("root")); | ||
| root.render( | ||
| <React.StrictMode> | ||
| <App /> | ||
| </React.StrictMode> | ||
| ); | ||
|
|
||
| // If you want to start measuring performance in your app, pass a function | ||
| // to log results (for example: reportWebVitals(console.log)) | ||
| // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals | ||
| reportWebVitals(); |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第二引数として監視する変数を指定すると副作用をスキップすることができる
https://ja.reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects