Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions data/db.json
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"
}
]
}
38 changes: 0 additions & 38 deletions src/App.css
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);
}
}
19 changes: 3 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import logo from './logo.svg';
import './App.css';
import "./App.css";
import TripList from "./components/TripList";

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<TripList />
</div>
);
}
Expand Down
8 changes: 0 additions & 8 deletions src/App.test.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/components/TripList.css
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;
}
38 changes: 38 additions & 0 deletions src/components/TripList.js
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]);

Copy link
Copy Markdown
Owner Author

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

useEffect(() => {
fetchTrips();
}, [fetchTrips]);
Comment on lines +12 to +14

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEffectの第二引数に関数を指定する場合、関数が定義されると発火する
Reactがrenderを行うたびにコンポーネント内の関数は再定義されるので、useCallbackを使用してrenderのたびにあたらしい関数を作らないようにしないと無限ループとなる。

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>
);
}
13 changes: 0 additions & 13 deletions src/index.css
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;
}
16 changes: 5 additions & 11 deletions src/index.js
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();
1 change: 0 additions & 1 deletion src/logo.svg

This file was deleted.

13 changes: 0 additions & 13 deletions src/reportWebVitals.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/setupTests.js

This file was deleted.