How do you change a style of a child when hovering over a parent using Material UI styles?

Below is an example of the correct syntax for v4 (“& $addIcon” nested within &:hover). Further down are some v5 examples. import * as React from “react”; import { render } from “react-dom”; import { Grid, makeStyles } from “@material-ui/core”; import AddIcon from “@material-ui/icons/Add”; const useStyles = makeStyles(theme => ({ outerDiv: { backgroundColor: theme.palette.grey[200], padding: … Read more

React props: Should I pass the object or its properties? Does it make much difference?

According to the principle of least privilege, this is a correct way: <InnerComponent name={object.name} image={object.image} /> This restricts InnerComponent from accidentally modifying original object or accessing properties that aren’t intended for it. Alternatively, properties could be picked from original object and passed as props: <InnerComponent {…pick(object, ‘name’, ‘image’)} /> If there are numerous properties that … Read more

Cannot find module ‘sass’

To note! node-sass is deprecated as by now! Warning: LibSass and Node Sass are deprecated. While they will continue to receive maintenance releases indefinitely, there are no plans to add additional features or compatibility with any new CSS or Sass features. Projects that still use it should move onto Dart Sass. Instead you can see … Read more

jest: Test suite failed to run, SyntaxError: Unexpected token import

Jest sets the env variable to test, so I had to add my presets to the env setting in .babelrc: { “plugins”: [“syntax-dynamic-import”, “transform-runtime”], “presets”: [ [ “es2015”, { “modules”: false } ], “react”, “stage-0” ], “env”: { “start”: { “presets”: [ “react-hmre” ] }, “test”: { “presets”: [“es2015”, “react”, “stage-0”] } } }

How to preload images in React.js?

Supposing you have pictures: string[]; – array of pictures urls defined in your component’s props. You should define componentDidMount() method in your component and then just create new Image object for each picture you want to be preloaded: componentDidMount() { this.props.pictures.forEach((picture) => { const img = new Image(); img.src = picture.fileName; }); } It forces … Read more

Get scroll position with Reactjs

In case you need to keep on track of the scroll position, you can use react hooks to do so, that way it’s possible to check the scroll position any time you need it: import React, { useState, useEffect } from ‘react’; … // inside component: const [scrollPosition, setScrollPosition] = useState(0); const handleScroll = () … Read more

Why would we use useEffect without a dependency array?

For this exact case you’re right because undefined is passed as the dependencies of useEffect. This means useEffect runs on every render and thus the event handlers will unnecessarily get detached and reattached on each render. function listener() { console.log(‘click’); } function Example() { const [count, setCount] = window.React.useState(0); window.React.useEffect(() => { console.log(`adding listener ${count}`); … Read more