Skip to content

Commit

Permalink
useRefを使って入力した値を取得する
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yoshiii committed Jun 9, 2022
1 parent 1d52701 commit c193ba3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 55 deletions.
37 changes: 1 addition & 36 deletions src/App.css
@@ -1,38 +1,3 @@
.App {
.container {
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);
}
}
32 changes: 14 additions & 18 deletions src/App.tsx
@@ -1,24 +1,20 @@
import React from 'react';
import logo from './logo.svg';
import './App.css';
import React, { useRef } from "react";
import "./App.css";
import Gallery from "./component/Gallery";

function App() {
const ref = useRef<HTMLInputElement>(null);
const handleSubmit = (e: React.FormEvent<HTMLFormElement>): void => {
e.preventDefault();
console.log(ref.current?.value);
};
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<div className="container">
<h1>My Pixabay</h1>
<form onSubmit={(e) => handleSubmit(e)}>
<input type="text" placeholder="Search" ref={ref} />
</form>
<Gallery />
</div>
);
}
Expand Down
16 changes: 16 additions & 0 deletions src/component/Gallery.tsx
@@ -0,0 +1,16 @@
import React from "react";

const Gallery = () => {
return (
<div className="gallery__wrap">
<div className="gallery__item">
<img
src="https://pixabay.com/get/gb40a71049feaebfd596b42da288af252a84b55f9917d81a47fee2e5adb0401c3fde97ce72c4d5c2899320b1d09cc255497be82cc8e0fc9db4cc181851ba66e71_1280.jpg"
alt=""
/>
</div>
</div>
);
};

export default Gallery;
1 change: 0 additions & 1 deletion src/logo.svg

This file was deleted.

0 comments on commit c193ba3

Please sign in to comment.