React Overview - ChoDragon9/posts GitHub Wiki

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ

JavaScript ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ง ๊ทธ๋ž˜๋„ "ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ"๋ผ๊ณ  ๋ช…๋ช…ํ•œ๋‹ค.

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

<Welcome name="Sara" />

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

<Welcome name="Sara" />

์ƒํƒœ

  • ์ƒํƒœ ๋ณ€๊ฒฝ์€ setState()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • this.state์€ constructor์—์„œ๋งŒ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
import React from 'react';
import ReactDOM from 'react-dom';

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()}
  }

  componentDidMount() {
    this.timerId = setInterval(
      () => this.tick(),
      1000
    )
  }

  componentWillUnmount() {
    clearInterval(this.timerId)
  }

  tick() {
    this.setState({
      date: new Date()
    })
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}</h2>
      </div>
    )
  }
}

ReactDOM.render(
  <Clock />,
  document.getElementById('root')
);

์ด๋ฒคํŠธ

function onClickButton(event) {}

<button onClick={onClickButton}></button>

<button onClick={(e) => this.deleteRow(id, e)}></button>

์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง

function UserGreeting (props) {
  return <h1>Welcome back!</h1>
}

function GuestGreeting (props) {
  return <h1>Please sign up.</h1>
}

function Greeting (props) {
  return props.isLoggedIn ? <UserGreeting /> : <GuestGreeting />
}

์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค!

function Greeting (props) {
  let button;
  if (props.isLoggedIn) {
    button = <UserGreeting />
  } else {
    button = <GuestGreeting />
  }
  return button
}

๋…ผ๋ฆฌ && ์—ฐ์‚ฐ์ž๋กœ if๋ฅผ ์ธ๋ผ์ธ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ

function Counter (props) {
  return (
    <div> { props.count && <h1>Message: {props.count}</h1>} </div>
  )
}

๋ฆฌ์ŠคํŠธ ์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง

const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) => (
  <li key={number.toString()}>{number}</li>
))

์ œ์–ด ์ปดํฌ๋„ŒํŠธ

  • React state๋กœ ํผ๊ณผ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ผญ onChange ์ด๋ฒคํŠธ๋กœ ๋ณ€๊ฒฝ๋œ ์ƒํƒœ๋ฅผ ๋ฐ›์•„ state๋ฅผ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•œ๋‹ค.
class NameForm extends React.Component {
  constructor(props) {
    super(props)
    this.state = {value: ''}
    this.handleBlur = this.handleBlur.bind(this)
    this.handleChange = this.handleChange.bind(this)
  }
  handleBlur () {
    console.log(this.state.value)
  }
  handleChange(event) {
    this.setState({value: event.target.value})
  }
  render() {
    return (<input
      type="text"
      value={this.state.value}
      onBlur={this.handleBlur}
      onChange={this.handleChange}
    />)
  }
}

PropTypes

import PropTypes from 'prop-types';

class Greeting extends React.Component {
  render() {
    return (<h1>Hello, {this.props.name}</h1>)
  }
}

Greeting.propTypes = {
  name: PropTypes.string
}
import PropTypes from 'prop-types';

function Greeting (props) {
  return (<h1>Hello, {props.name}</h1>)
}

Greeting.propTypes = {
  name: PropTypes.string
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ