-1

How can I style a component to lay out subcomponents in a grid?

I've been trying to do it multiple ways:

Method 1:

  1. Import custom CSS Stylesheet - in the parent component, I'm importing the stylesheet with: import './style.css' (not the actual file, but it get's the point across.)
  2. Target and apply styles: Should be as simple as .targetClassName{display:grid;grid-template-columns: repeat(4,1fr);}

In my experience, it doesn't make it to the DOM.

Method 2:

  1. Set a style as an object: const style = { display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)'};
  2. Apply inline to the component: <div className='items' style={this.style}></div>

for some reason isn't working as well.

Just for some background, this is the component:

import React, {Component} from 'react';
import Computer from './computer';

import './styles/computers.sass';


class Computers extends Component{

  deleteComputer(_id){
    this.props.onDelete(_id);
  }


  render(){
    const compListStyle = {
      display: 'grid',
      gridTemplateColumns: 'repeat(4, 1fr)'
    }
    let computerList;
    if(this.props.computers){
      computerList = this.props.computers.map(x => {
        console.log(x);
        return(
          <Computer computer={x} onDelete={this.deleteComputer.bind(this)}/>
        )
      })
    }
    return(
      <div className='computers' style={this.compListStyle}>
        {computerList}
      </div>
    );
  }
}

export default Computers;

2 Answers 2

1

You are using a class variable in styles object. So you should set it to one too.

this.compListStyle = {
  display: 'grid',
  gridTemplateColumns: 'repeat(4, 1fr)'
}

The first method should be working if you are using a style-loader/sass-loader to load the style files.

Sign up to request clarification or add additional context in comments.

1 Comment

Just Happy to Help
0

Actually, there are many ways to use CSS in react I just don't prefer you to keep your styles inside render() keep it out of your component

 const compListStyle = {
      display: 'grid',
      gridTemplateColumns: 'repeat(4, 1fr)'
    }

class Computers extends Component{ ...}

And use them like

 <div className='computers' style={compListStyle}>
    {computerList}
  </div>

As well when you're iterating data don't keep them in render()

   {this.props.computers && this.props.computers.map(x => {
                console.log(x);
                return (
                <div>
                <Computer computer={x} onDelete={this.deleteComputer.bind(this)}/>
                </div>)}
                )}

Here's final code very clean and small

const compListStyle = {
    display: 'grid',
    gridTemplateColumns: 'repeat(4, 1fr)'
};

class Computers extends Component {
    deleteComputer(_id) {
        this.props.onDelete(_id);
    }
    render() {
        return (
            <div>
                {this.props.computers && this.props.computers.map(x => {
                    console.log(x);
                    return (
                        <div className='computers' style={compListStyle}>
                            <Computer computer={x} onDelete={this.deleteComputer.bind(this)}/>
                        </div>)}
                )}
            </div>
        );
    }
}

export default Computers;

As well here are some tips

ReactJS Folder structures

Handle hundreds of images in ReactJS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.