Giter Site home page Giter Site logo

assets_and_cra's Introduction

Assets and CRA

To include images and CSS, you can import them in JS files.

The standard convention is to make your css file the same name as your component.

For example -> House.css for House component

Then we import it at the top of the House.js component - create-react-app will automatically load that CSS Conventional to add classNAme="House" onto House div (top level element) - and use that as prefix for sub-items to style:

<div className="House">
    <p className="House-title"> {this.props.title} </p>
    <p className="House-address"> {this.props.addr} </p>
</div>

We import the CSS but what we import doesn't apply only to the file. That's why we use specific classes, usually prefixed with the name of the component, to apply them selectively.

If we write general styles and we apply them everywhere, they'll apply all over the place. There are ways of scoping CSS rules, but it requires external tools.

Images

We import them with the same syntax as components

  • Store images in src/folder with the components. If there's many files, you can structure it however you want.

Conclussions

Importing assets is something that we do all the time in React.

In case you forget, you can always to refer to the App.js file that create-react-app give you:

import React, {Component} from 'react';
import logo from "./logo.svg"; // you're import an SVG
import "./App.css"; // you're importing a CSS file

class App extends Component {
    render() {
        return (
            <div className="App">
                <header className="App-header">
                    <img src={logo} className="App-logo" alt="logo" />
                    <p>
                        Edit <code>src/App.js</code> and save to reload.
                    </p>
                    <p>This React app is INCREDIBLE </p>
                </header>
            </div>
        );
    }
}
export default App;

What Colt does:

Every time he makes a new component Dog.js, <Dog />, he also makes a new CSS file with the same name Dog.CSS and then import it at the top of every component I create.

assets_and_cra's People

Contributors

mayanhavoc avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.