- mock: κ±°μ§λ, κ°μ§μ
- μ΄λ¦μμ μ μ μλ―μ΄ mock data λ κ°μ§ λ°μ΄ν°, μν λ°μ΄ν°
- μ¦, μ€μ API μμ λ°μμ¨ λ°μ΄ν°κ° μλ νλ‘ νΈμ€λ κ°λ°μκ° νμμ μν΄ μνμ©μΌλ‘ λ§λ λ°μ΄ν°
API κ° μμ§ μ€λΉμ€μΈ κ²½μ°
:: νλ‘μ νΈ μ§ν μ APIκ° λμ€κΈ° μ΄μ μ νμ΄μ§ λ μ΄μμμ΄ λ¨Όμ λμ€λ κ²½μ°κ° λ§λ€. μ΄λ¬ν κ²½μ°μλ λ°±μλμμ APIκ° λμ¬ λκΉμ§ 무μμ κΈ°λ€λ¦¬λ κ² μλλΌ, mock dataλ₯Ό λ§λ€μ΄ λ°μ΄ν°κ° λ€μ΄μ€λ μν©μ 미리 λλΉνκ³ UIκ° κΈ°νμ λ§κ² ꡬνλλμ§ λ¨Όμ νμΈν΄μΌ νλ€.
Backend μμ μνν μμ¬μν΅μ μν¨
:: μ£Όκ³ λ°λ λ°μ΄ν°κ° μ΄λ€ ννμΈμ§, key-value κ°μ 미리 λ§μΆ°λ³Ό μ μλ€.
π§· data.js μμΉ
ν΄λΉ μ»΄ν¬λνΈ λ°λ‘ μμ λ§λ€μ΄μ, λ°λ‘ μ κ·ΌνκΈ° μ½κ²!
const SlickData = [ { id: 1, skills: "HTML" }, { id: 2, skills: "CSS" }, { id: 3, skills: "JavaScript" }, { id: 4, skills: "React" }, { id: 5, skills: "React Native" }, { id: 6, skills: "Angular" }, { id: 7, skills: "Vue" }, { id: 8, skills: "Redux" }, { id: 9, skills: "TypeScript" } ]; export default SlickData;
π§· app.js
import React from 'react'; import DATA from 'data.js';
π§· data.json μμΉ
π§· data.json
{ "data": [ { "id": 1, "name": "yang", "age": 30 }, { "id": 2, "name": "kim", "age": 29 } ] }
π§· app.js
import React, { Component } from 'react' export class Main extends Component { constructor(){ super(); this.state = { data: [] }; } componentDidMount = () => { fetch('http://localhost:3002/data/data.json') .then(res => res.json()) .then(res => { console.log("firstly: ", res.data) this.setState({ data: res.data }, () => { console.log("secondly: ", this.state.data) }) }) } render() { console.log("thirdly: " , this.state.data) return ( <div> hello </div> ) } } export default Main