Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- charAt()
- substring()
- filter()
- 항해99솔직후기 #항해99장점 #항해99단점 #부트캠프추천
- reat if문
- setDate
- repeat()
- 소수점 올림내림
- getday()
- includes()
- useState()
- indexOf()
- Math.floor()
- jsx반복문
- React
- map()
- useEffect()
- toUpperCase()
- 차집합
- isNaN()
- Eventlitener
- sort()
- 교집합
- Number()
- parseInt()
- 3진수
- useRef()
- Math.sqrt()
- slice()
- new Date()
Archives
- Today
- Total
개발자로 전향중
React Hook: useref() 본문
만약에, 내가 어떤 인풋박스에서 텍스트를 가져오고 싶으면 어떻게 접근해야할까? DOM에서 다루는 방법은 1주차에 배웠는데, 리액트에선 어떻게 하면 좋을까요? (render()가 끝나고 가져오면 될까요? 아니면 mount가 끝나고? 아니, 그 전에 가상돔에서 가져오나? 아니면 DOM에서? 😖) → 답은, 리액트 요소에서 가져온다!
->React 요소를 가지고 오는 방법1 : React.createRef()
import React from "react";
import logo from "./logo.svg";
// BucketList 컴포넌트를 import 해옵니다.
// import [컴포넌트 명] from [컴포넌트가 있는 파일경로];
import BucketList from "./BucketList";
import styled from "styled-components";
// 클래스형 컴포넌트는 이렇게 생겼습니다!
class App extends React.Component {
constructor(props) {
super(props);
// App 컴포넌트의 state를 정의해줍니다.
this.state = {
list: ["영화관 가기", "매일 책읽기", "수영 배우기"],
};
// ref는 이렇게 선언합니다!
this.text = React.createRef();
}
componentDidMount(){
// 콘솔에서 확인해보자!
console.log(this.text);
console.log(this.text.current);
}
// 랜더 함수 안에 리액트 엘리먼트를 넣어줍니다!
render() {
return (
<div className="App">
<Container>
<Title>내 버킷리스트</Title>
<Line />
{/* 컴포넌트를 넣어줍니다. */}
{/* <컴포넌트 명 [props 명]={넘겨줄 것(리스트, 문자열, 숫자, ...)}/> */}
<BucketList list={this.state.list} />
</Container>
<div>
<input type="text" ref={this.text}/>
</div>
</div>
);
}
}
const Container = styled.div`
max-width: 350px;
min-height: 80vh;
background-color: #fff;
padding: 16px;
margin: 20px auto;
border-radius: 5px;
border: 1px solid #ddd;
`;
const Title = styled.h1`
color: slateblue;
text-align: center;
`;
const Line = styled.hr`
margin: 16px 0px;
border: 1px dotted #ddd;
`;
export default App;
->React 요소를 가지고 오는 방법2 : React.useRef()
import React from "react";
import styled from "styled-components";
const BucketList = ({ list }) => {
const my_lists = list;
const my_wrap = React.useRef(null);
console.log(my_wrap); // 콘솔로 확인해봐요!
window.setTimeout(() => { // 1초 뒤에는?!
console.log(my_wrap);
}, 1000);
return (
<div ref={my_wrap}>
{my_lists.map((list, index) => {
return <ItemStyle key={index}>{list}</ItemStyle>;
})}
</div>
);
};
const ItemStyle = styled.div`
padding: 16px;
margin: 8px;
background-color: aliceblue;
`;
export default BucketList;
'React' 카테고리의 다른 글
리액트에서 자주쓰는 if문 작성패턴 5개 (0) | 2022.01.22 |
---|---|
react(리액트) JSX 배열 반복문 렌더링 - for/map/forEach (0) | 2022.01.22 |
React router dom v6 (0) | 2022.01.22 |
React Hook: useEffect(), EventListener (0) | 2022.01.22 |
React Hook: useState(함수) (0) | 2022.01.22 |