How can I style a component to lay out subcomponents in a grid?
I've been trying to do it multiple ways:
Method 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.) - 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:
- Set a style as an object:
const style = { display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)'}; - 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;