Asif Saeed
Asif Saeed

Reputation: 2045

Font not loading in Reactjs

This is how I am loading my Font files in reactjs

import styled from 'styled-components';
import fontUrls from './UberMove-Bold.ttf';
import fontUrls1 from './UberMove-Light.ttf';
import fontUrls2 from './UberMove-Medium.ttf';
import fontUrls3 from './UberMove-Regular.ttf';

const Fonts = styled`
    @font-face {
       font-familty: 'UberMove-Light';
       src: url(${fontUrls1}) format('truetype');
    }`;

This throws an error while importing

Uncaught Error: Cannot create styled-component for component: @font-f .ace{font-familty:'UberMove-Light';src:url(,) format('truetype');}.

Upvotes: 3

Views: 6842

Answers (1)

Clarity
Clarity

Reputation: 10873

You need to load the font-face globally and then use it in the components:

import { createGlobalStyle } from "styled-components";

const GlobalStyles = createGlobalStyle`
  body {
    @import url(${fontUrls1});
    font-family: 'UberMove-Light';
  }
`
const App = () => (
  <div>
    <GlobalStyles />

   //...

  </div>
)

Upvotes: 5

Related Questions