최신 패턴
import React from 'react'
import ReactDOM from 'react-dom'
function hellowWorldButton() {
return React.createElement('button',
{ onClick:() => {}},
"Hello World!"
)
}
const rootContainer = document.getElementById('react-root')
ReactDOM.render(React.createElement(helloWorldButton), rootContainer)
function returnOneOneArray() {
return [1, 1];
}
const [a,b] = returnOneOneArray() // a=1, b=1
.useState() : 컴포넌트의 상태를 설정하는 메서드
- 내부 파라미터는 초기값을 의미합니다.
import React from 'react'
import ReactDOM from 'react-dom'
function hellowWorldButton() {
const [isClick, setClickState] = React.useState(false)
console.log(isClick) //false
setClickState(true)
console.log(isClick) //true
return React.createElement('button',
{ onClick:() => {}},
"Hello World!"
)
}
컴포넌트 활용
import React from 'react'
import ReactDOM from 'react-dom'
function hellowWorldButton() {
const [isClick, setClickState] = React.useState(false)
const text= isClick ? "Bye World!" : "Hello World!"
return React.createElement('button',
{ onClick:() => setClickState(!isClick)},
text
)
}
import React from 'react'
import ReactDOM from 'react-dom'
function hellowWorldButton() {
const [isClick, setClickState] = React.useState(false)
const text= isClick ? "Bye World!" : "Hello World!"
return (
<button onClick={() => setClickState(!isClick)}>
<div>
<span> Hello Wolrd! </span>
<div>
</button>
)
}
npx create-react-app 파일이름
cd 파일이름
npm start
npm run build // 파일 압축
npx serve -s build // 서버를 생성하고 해당 파일 이름을 루트 파일로 지정함
// app.js
return(
<div className = "App">
<MainHeader text = "Hello World!"> </MainHearder>
</div>
)
// MainHeader.js
function MainHeader({text}){
return <h1>{text}</h1>
}
// MainHeader.js
function MainHeader({props}){
return <h1>{props.text}</h1>
}
function App() {
const array1 = ['apple', 'banana', 'orange']
return {
<div className = "App">
<ul>
{array1.map((value, index) => {
return (
<CustomList text ={ ${index}, ${value}}></CustomList>
)
})}
</ul>
</div>
}
}
기본 스타일링 적용 방법
sass를 이용한 스타일링 적용 방법
npm install node-sass
해당 태그의 자식에게 적용시키는 방법
& > span {
display : flex;
}
npm i styled-components
import styled from styled-components
function component (props){
return <h1>props.text</h1>
}
function App() {
const [text, setText] = useState("Hello")
const buttonClick = () => {
text === "Hello" ? setText("World") : setText("Hello")
}
const conditionRendering = text === "World" ? (
<MainHeader text = "World"> </MainHeader>
) : (
<MainHeader text = "Hello"> </MainHeader>
)
return (
<div className = "App">
<h1>조건부 렌더링</h1>
(conditionRendering)
<button onClick = buttonClick>{text}</button>
)
}
function App() {
const [name, setName] = useState({
first : 'Hyeseong',
last : "Oh"
})
const firstNameRef = useRef() // reference로 HTML 레퍼런스를 참조한다.
const lastNameRef = useRef()
const {first, last} = name // 비구조화 할당
const confirm = () => {
setName({
first : firstNameRef.current.value,
last : lastNameRef.current.value
})
}
const addItem = (e) => {
const inputText = e.target.value
setText(inputText)
}
const onChange = () => {
}
return (
<div className = "App">
<h1> {last} {first} </h1>
<input name = "성" placeholder="성" ref={lastNameRef}> </input>
<input name = "이름" placeholder="이름" ref={firstNameRef}> </input>
<button onClick = {confirm}>확인</button>
</div>
)
}