github
에서 수행하기 때문에 organization에서 week-1
의 브랜치를 생성했다.Radio.jsx
만들기import React , {useState} from 'react';
function Radio(){
const [nowSelect, setNowSelect] = useState(null);
const onSelect = (e) => {
console.log(e.target.name);
setNowSelect(e.target.value);
}
return (
<div>
<h1>{nowSelect}</h1>
<label>
<input type="radio" onChange={onSelect} name="radioGroup" value='1'/> 1번
</label>
<label>
<input type="radio" onChange={onSelect} name="radioGroup" value='2'/> 2번
</label>
</div>
)
}
export default Radio;
useRef
를 이용하여 상태를 관리하는 Radio
컴포넌트를 만들었다.Checklist.jsx
만들기import React, { useRef } from 'react';
const Checklist = () => {
const checkRef = useRef([]); //checklist 상태를 담을 배열로 초기화
const handleCheckboxChange = (index) => {
const setChecklist = [...checkRef.current];
setChecklist[index] = !setChecklist[index]; //true false 전환
checkRef.current = setChecklist;
};
const handleSubmit = () => { //prevent 사용시 e 파라미터로 받기
//e.preventDefault(); //나중에 원하는 사용을 위해 주석 처리.
console.log('Checklist:', checkRef.current); //현재는 확인만을 위해 출력만 담당.
};
return (
<form onSubmit={handleSubmit}>
<label>
<input
type="checkbox"
checked={checkRef.current[0]} //true fales 전환값 적용
onChange={() => handleCheckboxChange(0)}
/>
Option 1
</label>
<label>
<input
type="checkbox"
checked={checkRef.current[1]}
onChange={() => handleCheckboxChange(1)}
/>
Option 2
</label>
<label>
<input
type="checkbox"
checked={checkRef.current[2]}
onChange={() => handleCheckboxChange(2)}
/>
Option 3
</label>
<button type="submit">Submit</button>
</form>
);
};
export default Checklist;
.current
가 아닌 [value, setFuntion]
꼴로 쓰고 싶었는데 자꾸 오류가 났다.Toggle.jsx
구현import React, { useRef } from 'react';
const Toggle = () => {
const toggleRef = useRef(false);
const handleToggle = () => {
toggleRef.current = !toggleRef.current; //true, false 반전
console.log(toggleRef.current);
};
return (
<div>
<h1>{toggleRef.current}</h1>
<button onClick={handleToggle}>
Button
</button>
</div>
);
};
export default Toggle;
마찬가지로 .current
사용하기 싫어서 난리쳤지만 결국 포기하고 사용해버렸다.
Carousel
만들기
버튼을 눌렀을 때 슬라이드가 넘어가는 UI
import React, { useState } from 'react';
const Carousel = () => {
const [currentIndex, setCurrentIndex] = useState(0);
const images = ['1.jpg', '2.jpg', '3.jpg'];
const handlePrevious = () => {
setCurrentIndex((prevIndex) => (prevIndex === 0 ? images.length - 1 : prevIndex - 1));
}; //3항 연산자를 통해 0이 될 경우 마지막 사진으로 넘기는 방식
const handleNext = () => {
setCurrentIndex((prevIndex) => (prevIndex === images.length - 1 ? 0 : prevIndex + 1));
};
return (
<div className="carousel">
<div className="carousel-images">
<img src={images[currentIndex]} alt={`Image ${currentIndex + 1}`} />
</div>
<button onClick={handlePrevious}>Prev</button>
<button onClick={handleNext}>Next</button>
</div>
);
};
export default Carousel;
Toast
만들기
토스트라는 걸 처음 들어봤는데 뭔가 익숙한.. 버튼 클릭 시 올라왔다가 사라지는 UI를 만들면 된다고 알려주셨다.
import React, { useRef } from 'react';
const Toast = () => {
const toastRef = useRef(null);
const showToast = () => {
if (toastRef.current) {
toastRef.current.style.display = 'block';
setTimeout(() => {
toastRef.current.style.display = 'none';
}, 1000);
}
};
return (
<div>
<button onClick={showToast}>회원가입 하기</button>
<div ref={toastRef} style={{ display: 'none' }}>
회원가입 완료 !
</div>
</div>
);
};
export default Toast;
여기서 중요한건 버튼을 클릭했을 경우에만 div
가 보여야하기에 기본적으로 display
를 none
으로 설정 후 클릭 시 마다 ref를 통해 display 값을 변경시켜주는 것이다.
breadcrumb
만들기
개인적으로 처음 들어본 형태의 UI여서 당황했다.
실제로 제일 못만든 것 같은 UI이기도 하고 너무 조잡하게 만든 것 같다.
import React from 'react';
const Breadcrumb = () => {
const items = [{text:"hi"}, {text:"bye"}];
return (
<nav>
<ol className="breadcrumb">
{items.map((item, index) => (
<li key={index} className={`breadcrumb-item${index === items.length - 1 ? ' active' : ''}`}>
{index === items.length - 1 ? (
<span>{item.text}</span>
) : (
<a href={item.text}>{item.text}</a>
)}
</li>
))}
</ol>
</nav>
);
};
export default Breadcrumb;
이걸 모두 실행할 App.js
만들기