[shop-project] import / export

kirin.logยท2021๋…„ 4์›” 27์ผ
0
post-custom-banner

๐ŸŒˆ import / export

  • ํŒŒ์ผ์„ ์ชผ๊ฐค ๋•Œ ํ™œ์šฉํ•˜๋Š” import / export ๋ฌธ๋ฒ•
  • component๋ฅผ ํŒŒ์ผ๋กœ ์ชผ๊ฐœ์„œ ์„œ๋กœ ์—ฐ๊ฒฐํ•  ๋•Œ, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— export ํ•ด์ฃผ๊ณ  ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— ๋ถ™์—ฌ๋„ฃ์„ ๋•Œ๋Š” import ํ•ด์ค€๋‹ค.
  • data์ž๋ฃŒ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ ๋•Œ, ๋‹ค๋ฅธ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•  ๊ฒฝ์šฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ ์šฉํ•œ๋‹ค.

๐ŸŽ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

1. ๋‚ด๋ณด๋‚ด๊ธฐ

export default

โ— export default๋Š” ํ•œ ํŒŒ์ผ๋‹น 1๊ฐœ ๋ฐ–์— ๋ชป์“ด๋‹ค

2. ๊ฐ€์ ธ์˜ค๊ธฐ

import ๋ณ€์ˆ˜๋ช…์ž‘๋ช… from './๊ฒฝ๋กœ' (ํŒŒ์ผ๊ฒฝ๋กœ from๋’ค์— ๋ถ™์ด๊ธฐ)

โ— ๋‹ค๋ฅธ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ, ๊ฒฝ๋กœ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋ฌด์กฐ๊ฑด ./ ๋กœ ์‹œ์ž‘ํ•œ๋‹ค

3. ๋ถˆ๋Ÿฌ์˜จ ํŒŒ์ผ์„ ํ™œ์šฉํ•˜๊ธฐ

return๋ฌธ ๋‚ด์—์„œ ์ค‘๊ด„ํ˜ธ({ })์•ˆ์— ์ž‘๋ช…ํ•œ ๋ณ€์ˆ˜๋ช…์„ ๊ฐ€์ ธ๋‹ค ์“ด๋‹ค
{์ž‘๋ช…ํ•œ ๋ณ€์ˆ˜๋ช…}

return๋ฌธ ๋ฐ–์—์„œ ์ž‘๋ช…ํ•œ ๋ณ€์ˆ˜๋ช…๋งŒ ๊ทธ๋Œ€๋กœ ๋ถ™์—ฌ ์“ด๋‹ค(์ค‘๊ด„ํ˜ธ์—†์ด)
let [state, setState] = useState(์ž‘๋ช…ํ•œ๋ณ€์ˆ˜๋ช…)


๐Ÿฃ data.jsํŒŒ์ผ ์™ธ๋ถ€ํŒŒ์ผ๋กœ ๋นผ์„œ ์—ฐ๊ฒฐํ•˜๊ธฐ

// data.js ํŒŒ์ผ
export default [
    {
        id: 0,
        title: "White and Black",
        content: "Born in France",
        price: 120000
    },

    {
        id: 1,
        title: "Red Knit",
        content: "Born in Seoul",
        price: 110000
    },

    {
        id: 2,
        title: "Grey Yordan",
        content: "Born in the States",
        price: 130000
    }
]



// App.js ํŒŒ์ผ
import React, { useState } from 'react';
import Data from './data.js';  // import ํ•ด์„œ data.jsํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค

function App() {

  let [shoes, setShose] = useState(Data);
  // shoes๋ผ๋Š” state์•ˆ์—๋Š” data.js์•ˆ์— ๋‹ด๊ฒจ์žˆ๋Š” object์ž๋ฃŒ๊ฐ€ ๋ชจ๋‘ ๋‹ด๊ฒจ์žˆ๋‹ค

  return (
    <div className="col-md-4">
        <h4> { shoes[0].title } </h4>  // data.js์•ˆ์˜ ๋ฐ์ดํ„ฐ ์ค‘ title
        // arrayํ˜•ํƒœ๋‹ˆ๊น indexing ํ•ด์ฃผ๊ธฐ!!!
        <p> { shoes[0].content } & { shoes[0].price } </p>  
    </div>
  )
}

๐Ÿฃ App.jsํŒŒ์ผ ๋‚ด์šฉ ์™ธ๋ถ€ Component๋กœ ๋ถ„๋ฆฌํ•œ ๋’ค ์ถ•์•ฝํ•˜๊ธฐ(๋ฐ˜๋ณต๋ฌธ)

// App.js ํŒŒ์ผ
import React, { useState } from 'react';
import Data from './data.js';  
import Product from './component.Product.js'; // product.js ํŒŒ์ผ ๊ฐ€์ ธ์˜จ๋‹ค

function App() {

  let [shoes, setShose] = useState(Data);

  return (
    <Product shoes={shoes[0]} />  // ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ(product.js)๋กœ 0๋ฒˆ์งธ state ๋ณด๋‚ด์ฃผ๊ธฐ
    <Product shoes={shoes[1]} />  // 1๋ฒˆ์งธ state ๋ณด๋‚ด์ฃผ๊ธฐ
    <Product shoes={shoes[2]} />  // 2๋ฒˆ์งธ state ๋ณด๋‚ด์ฃผ๊ธฐ
    // ํ™”๋ฉด์— Product์ปดํฌ๋„ŒํŠธ๋ฅผ 3๊ฐœ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์–ด์„œ 3๊ฐœ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ(๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ถ•์•ฝํ•„์š”)
  )
}



// Product.js ํŒŒ์ผ
import React from 'react';

function Product(props) {   // props๋กœ ๋ฐ›๊ธฐ

  return (
    <div className="col-md-4">
        <h4> { props.shoes.title } </h4> 
        <p> { props.shoes.content } & { props.shoes.price } </p>  
    </div>
  )
}
export default Product

์œ„ ๋‚ด์šฉ์€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ(App.js)์˜ ๋ฐ์ดํ„ฐ์ธ shoes์˜ state๋ฅผ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ(Product.js)์— props๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.
๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ <Product />๋ฅผ 3๊ฐœ๋ฅผ ์ƒ์„ฑ ํ›„, ๊ฐ state์˜ ์ธ๋ฑ์Šค๋ณ„๋กœ ์ „๋‹ฌํ•œ๋‹ค. ์ฆ‰, ์ž๋ฃŒ์˜ index๋งŒํผ ๋ณต์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ณ , ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” indexing์—†์ด ๊ทธ๋ƒฅ ๋ฐ›๋Š”๋‹ค. <Product shoes={shoes[0]}๋Š” 0๋ฒˆ์งธ index๋ฅผ ๊ฐ€์ง„ ์ •๋ณด๋งŒ ์ „๋‹ฌ, <Product shoes={shoes[1]}๋Š” 1๋ฒˆ์งธ index๋ฅผ ๊ฐ€์ง„ ์ •๋ณด๋งŒ ์ „๋‹ฌ ํ•˜๋Š” ๋“ฑ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ index์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ index์— ๋”ฐ๋ผ ๋ณ„๋„๋กœ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” indexing์—†์ด ๊ธฐ๋ณธ ํƒœ๊ทธ๋กœ๋งŒ ๋ฐ›๋Š”๋‹ค.

// App.js ํŒŒ์ผ ๋‚ด <Product> ์ปดํฌ๋„ŒํŠธ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋Œ๋ฆฌ๊ธฐ
import React, { useState } from 'react';
import Data from './data.js';  
import Product from './component.Product.js';

function App() {

  let [shoes, setShose] = useState(Data);

  return (
    // shoes state๋ฅผ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฐ๋‹ค (return๋ฌธ ์•ˆ์˜ ๋ฐ˜๋ณต๋ฌธ์€ ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ค€๋‹ค!!)
   {
      shoes.map ( (a, i) => {
        return <Product shoes={ shoes[i] } key={i} />  // shoes[0], shoes[1], shoes[2]  
      })
    }

  )
}
profile
boma91@gmail.com
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€