ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

import {createGlobalStyle} from 'styled-components';
import reset from 'styled-reset';

const GlobalStyles = createGlobalStyle`
    ${reset};

	( ... )
    
@import url('https://fonts.googleapis.com/css2?family=Lobster&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400&display=swap');

	( ... )
}
`;

export default GlobalStyles;

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ ˆ๋ฒจ ์Šคํƒ€์ผ์ƒ์—์„œ ๊ณตํ†ต ์›น ํฐํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด styled-components์˜ createGlobalStyle ๋‚ด์—์„œ import๋ฌธ์„ ํ†ตํ•ด ํฐํŠธ๋ฅผ ์ ์šฉํ•ด์ฃผ์—ˆ๋‹ค.

 

dev-server๋ฅผ ํ†ตํ•ด importํ•œ ์›น ํฐํŠธ๊ฐ€ ๋ฌธ์ œ ์—†์ด ์ ์šฉ๋จ์„ ํ™•์ธํ•˜์˜€์ง€๋งŒ, ์•„๋ž˜์™€ ๊ฐ™์€ Warning Message๊ฐ€ ์ฝ˜์†” ์ฐฝ์—์„œ ๊ณ„์† ๋‚˜์™”๋‹ค.

Waring Message

์š”์ง€๋Š” @import CSS ๊ตฌ๋ฌธ์„ createGlobalStyle ์ƒ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋‹ค๋Š” ๋‚ด์šฉ์ด์—ˆ๋‹ค.

(๋ฒˆ๋“ค๋ง production ๋‹จ๊ณ„์—์„œ๋Š” CSSOM ๊ด€๋ จ APIS๋ฅผ ์ œ๋Œ€๋กœ ์ปจํŠธ๋กคํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค)

 

์ด์— createGlobalStyle๋ฅผ ๋Œ€์‹ ํ•˜์—ฌ React helmet์œผ๋กœ ๋ฆฌํŒฉํ† ๋งํ•˜๊ณ ์ž ํ–ˆ๋‹ค.

 

Refactor_helmet์„ ํ†ตํ•œ ์›น ํฐํŠธ ์ „์—ญ ์ ์šฉ

https://www.npmjs.com/package/react-helmet

 

react-helmet

A document head manager for React

www.npmjs.com

This reusable React component will manage all of your changes to the document head.

react-helmet์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ํ—ค๋” ๋ถ€๋ถ„์— ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด์— HelmetComponent๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋”ฐ๋กœ ๋ถ„๋ฆฝํ•ด์ฃผ์–ด, Root Component ์ƒ์—์„œ importํ•˜์—ฌ ์‚ฌ์šฉํ•ด์ฃผ์—ˆ๋”๋‹ˆ ๋ฌธ์ œ ์—†์ด ์›น ํฐํŠธ๋ฅผ ์ „์—ญ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

import React, {Component} from 'react';
import {Helmet} from 'react-helmet';

class HelmetComponent extends Component {
    render() {
        return (
            <Helmet>
        <link rel="icon" href="./img/instagram.png"/>
        <link rel="preconnect" href="https://fonts.googleapis.com"/>
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/>
        <link href="https://fonts.googleapis.com/css2?family=Lobster&display=swap" rel="stylesheet"/>

        <link rel="preconnect" href="https://fonts.gstatic.com"/> 
        <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet"/>

        <script src="https://kit.fontawesome.com/3090fba733.js" crossorigin="anonymous"/>
            </Helmet>
        )
    }
}

export default HelmetComponent; //๋ณ„๋„ ์ปดํฌ๋„ŒํŠธ๋กœ ์ƒ์„ฑ
	( ... )

class App extends Component {
  render() {
    return (
      <>
      <HelmetComponent/> //Root Component์— ์ ์šฉ
      <Router>
        <Switch>
        <Route exact path='/' component={Login}/>
        <Route exact path='/main' component={Main}/>  
        </Switch>
      </Router>
      </>
    )
  }
}

export default App;

 

common style๋กœ import

createGlobalStyle์ด๋‚˜ helmet์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด common style์— ์ „์—ญ์ ์œผ๋กœ ์„ ์–ธํ•ด์ฃผ์–ด๋„ ์›น ํฐํŠธ๋ฅผ ํ”„๋กœ์ ํŠธ ์ƒ์—์„œ ๋˜‘๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ํ™•์ธํ•˜์˜€๋‹ค.

@import url('https://fonts.googleapis.com/css2?family=Lobster&display=swap');

@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css');

* {
  box-sizing: border-box;
}

	( ... )
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/03   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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 29
30 31
๊ธ€ ๋ณด๊ด€ํ•จ