What is the best way to grasp the concepts behind MaterialUI styled components?

Can someone please help me understand how to apply styles to a component using MaterialUI? I've been reading the documentation but it's all very confusing to me. What exactly are classes and how do I connect the const style to the BeerList component?

I keep getting an error in my code saying "Cannot read property of classes undefined." I know I must be passing the wrong props, but I'm not sure how to resolve it...

 import React from 'react';
import BeerListItem from './BeerListItem';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import GridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';
import IconButton from '@material-ui/core/IconButton';
import StarBorderIcon from '@material-ui/icons/StarBorder';

const styles = theme => ({
    root: {
      display: 'flex',
      flexWrap: 'wrap',
      justifyContent: 'space-around',
     overflow: 'hidden',
     backgroundColor: theme.palette.background.paper,
    },
    gridList: {
      width: '100%',
      height: '100%',
      transform: 'translateZ(0)',
    },
    titleBar: {
      background:
        'linear-gradient(to bottom, rgba(0,0,0,0.7) 0%, ' +
        'rgba(0,0,0,0.3) 70%, rgba(0,0,0,0) 100%)'
      },

    icon: {
      color: 'white',
    },
  });

const BeerList = ({beers}) =>{
const {classes} = beers;
    const beerItems = beers.map((beer) => {
        return <BeerListItem  key={beer.id} beer = {beer}/> 
      });
      return (<div className={classes.root} >
       <GridList cellHeight={250} spacing={1} >
      {beerItems}
      </GridList>
      </div>);
    };


export default withStyles(styles)(BeerList);

Answer №1

To improve your components, make sure to destructure the classes from props:

const BeerList = (props) =>{
const {classes, beers} = props;

  // rest of your code
  return <div className={classes.root} >
    <GridList cellHeight={250} spacing={1} >
      {beerItems}
    </GridList>
  </div>
};

Just a small adjustment and you're good to go!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoiding memory leaks in React with the useEffect hook's second argument

While working on my project, I encountered an issue with fetching data inside of the useEffect hook in React. The goal was to update a state variable using the fetched data, but I kept getting null values even though the console log displayed the data co ...

Ways to designate choices as chosen

Is there a way to set the selected attribute for options inside the <SelectArrayInput> component? I haven't been able to figure out how to do that yet. I've attempted adding the selected attribute, but it doesn't seem to be working. ...

Set the height of the unordered list (ul) to be

I came across a design on Facebook that resembles a timeline, but I am having trouble adjusting the height of the ul element. <ul class="timeline">...</ul> I want the line in the middle to fill the height of the page even when there is less c ...

How to display only the even numbers in a numeric array with ReactJs

I am currently facing a challenge with extracting even numbers from a numeric array in ReactJs. While it is a simple task in languages like Python, I seem to be struggling with implementing the logic correctly in my React code. Here's what I have so f ...

How to direct all wildcard paths to a particular route in Next.js

I currently have a single landing page application built with nextJs. I am wondering if it is possible to redirect all paths to specific routes, similar to how we do it in react-router. How can I achieve the same functionality in nextJs? <BrowserRou ...

Bring in exclusively typescript module declarations

In my various React projects, I find myself constantly declaring the same typescript modules, such as fonts.d.ts: declare module "*.woff"; declare module "*.woff2"; or images.d.ts: declare module "*.jpg" { const src: string ...

Warning: GraphQL has detected a failed prop type error. The prop `query` is invalid as it is being supplied as an object rather than a string in the `StaticQuery

I have encountered a strange issue while working on my Gatsby website. When I try to visit a single blog post, the following error related to my Query pops up: index.js:2177 Warning: Failed prop type: Invalid prop `query` of type `object` supplied to `Sta ...

Can we set up two different conditions within the Axios .catch method to handle different states?

When attempting to update two states within the axios.post method, I face an issue where upon clicking the login button, the page goes blank. Interestingly, if I comment out or remove one of the state updates - either setEmptyFields(error.response.data.e ...

Proper method for positioning text in a line

Trying to recreate the image below, but facing alignment issues with the text in my code. How can I vertically align the text so that they are aligned like in the photo? Flexbox hasn't helped due to varying text lengths causing misalignment. const ...

Prevent divs from jumping to a new line with the float property

I've been busy working on a responsive webpage and so far, everything is going smoothly. The only issue I'm facing is that when the browser window size decreases and reaches a certain point, the div elements jump to the next line. Take a look at ...

Is there a way to make my HTML file search beyond its current directory?

My HTML file seems to be having trouble searching in a different folder for the CSS file that I've linked. How can I make it search in the correct folder? I have my HTML file in a directory named INDEX and the CSS file in another directory named STYL ...

What is the best way to reveal and automatically scroll to a hidden div located at the bottom of a webpage?

Whenever the image is clicked, I want to display a hidden div with effects like automatically scrolling down to the bottom This is what my code looks like: $(document).ready(function() { $('#test').click(function() { $(&apos ...

Using either prop type for a React component in Typescript

Looking to build a Table component that can either render data from a prop or accept custom rendering via children. In order to achieve this, I need a type that can handle both scenarios with either data or children. I've come across some solutions a ...

"Revolutionizing online payments with Material UI's dynamic payment

Struggling with creating dynamic material UI payment elements, I wanted to share how I successfully achieved this. You can access the code for these elements on the material UI website. https://i.stack.imgur.com/kYewa.png Below is an example of static da ...

Using React to fetch data from an API

Imagine having the following fetch request. useEffect(() => { fetch("apiUrl", { headers: { "Content-Type": "application/json", Accept: "application/json", }, }) .then((re ...

The fixed background-attachment feature does not function properly in Chrome when it is contained within a scrolling div

Essentially, if a background image is placed within a scrolling div, it will no longer remain fixed and will revert back to scrolling: CSS: <div class="wrapper"> <span></span> </div> HTML: html, body{ width: 100%; height: ...

I'm looking for some help with creating a visualization using JavaScript or Python. Can anyone offer some guidance?

// Defining the dimensions and margins of the graph var width = 460 var height = 460 // Appending the svg object to the body of the page var svg = d3.select("#my_dataviz") .append("svg") .attr("width", width) .attr("height", height) // Reading ...

Custom Pug design without any CSS files added

I am having trouble with my .pug template in my express project as it is not including its stylesheets. I have referred to the pug documentation but still can't figure out the issue. Any help would be appreciated! FILE TREE: views `-- index.pug `-- ...

Is it possible to design and implement Materialize CSS inputs without relying on Materialize CSS?'

Is there a method to implement Materialize CSS input elements that include placeholders and icons without directly using the Materialize CSS framework? I prefer not to mix frameworks in order to avoid potential conflicts, as I am already utilizing anothe ...

Using null or undefined for ternary operator formatting in React applications

When styling a component, what should be used if a specific condition is not fulfilled: null, undefined, or something else? For example: errorStyle: { right: locale === Locales.ARABIC ? 0 : null, left: locale !== Locales.ARABIC ? 0 : null, .. ...