ReactCSSTransitionGroup in child component not working - reactjs
I am trying to map over state and add a transition to each element so that the elements appear on mounting the dom.
There are two components relevant to this: PricingPage and PricingCard.
The code for PricingPage is below:
import React, {Component} from 'react';
import PricingCard from './PricingCard';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
class PricingPage extends Component {
constructor(props) {
super(props);
this.state={
pricingContent:[{title:'1Abstract Art Pricing', text:`Each piece of artwork is individually priced. I base this on how much work and effort I put into the picture, basically I'm just using my own criteria.`},{title:'2Abstract Art Pricing', text:`Each piece of artwork is individually priced. I base this on how much work and effort I put into the picture, basically I'm just using my own criteria.`},{title:'3Abstract Art Pricing', text:`Each piece of artwork is individually priced. I base this on how much work and effort I put into the picture, basically I'm just using my own criteria.`},{title:'4Abstract Art Pricing', text:`Each piece of artwork is individually priced. I base this on how much work and effort I put into the picture, basically I'm just using my own criteria.`},{title:'5Abstract Art Pricing', text:`Each piece of artwork is individually priced. I base this on how much work and effort I put into the picture, basically I'm just using my own criteria.`},{title:'6Abstract Art Pricing', text:`Each piece of artwork is individually priced. I base this on how much work and effort I put into the picture, basically I'm just using my own criteria.`}]
}
}
render () {
return (
<div className='pricing-page-wrapper'>
<div className='navbar-background'></div>
<h1 className='pricing-title'>Pricing</h1>
<ReactCSSTransitionGroup transitionName="pricing" transitionEnterTimeout={700} transitionLeaveTimeout={700}>
<div className='pricing-wrapper'>
{this.state.pricingContent.map((item,i) => {
return(
<PricingCard key={item.title} title={item.title} text={item.text} />
)
})}
</div>
</ReactCSSTransitionGroup>
</div>
)
}
}
export default PricingPage;
.pricing-page-wrapper{
min-height: 75vh;
width:80%;
margin: 0 auto;
.pricing-title{
color:#949494;
}
.pricing-wrapper{
opacity:1;
}
.pricing-enter {
height: 0px;
opacity: 0;
}
.pricing-enter.pricing-enter-active {
height: 90px;
opacity: 1;
transition: 700ms;
}
.pricing-leave {
opacity: 1;
height: 90px;
}
.pricing-leave.pricing-leave-active {
opacity: 0;
height: 0;
transition: 700ms;
}
}
PricingCard page:
import React, {Component} from 'react';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
class PricingCard extends Component {
constructor(props){
super(props);
}
render () {
return (
<div className='pricing-card'>
<h3 className='pricing-card-title'>{this.props.title}</h3>
<div className='pricing-card-text'>{this.props.text}</div>
</div>
)
}
}
export default PricingCard;
.pricing-card{
border-radius: 15px;
border: 1px solid #949494;
padding: 20px;
height:90px;
margin: 10px 0 10px 0;
.pricing-card-title{
color:#949494;
}
.pricing-card-text{
color:#949494;
}
}
I cant seem to figure out what I'm doing wrong, any help would be appreciated.
You must use some unique key for an item in list, it's array index won't work. You can use item.title for example.
You must wrap all the elements group into single ReactCSSTransitionGroup, not every individual element.
ReactCSSTransitionGroup will track them by their keys, so it must be mounted and have all that unique keys for group elements. So take it out of PricingCard.render to PricingPage.render.
class PricingPage extends Component {
render () {
return (
<div className='pricing-page-wrapper'>
<div className='navbar-background'></div>
<h1 className='pricing-title'>Pricing</h1>
<ReactCSSTransitionGroup transitionName="pricing"
transitionEnterTimeout={700} transitionLeaveTimeout={700}>
{this.state.pricingContent.map((item,i) => {
return(
<PricingCard key={item.title}
title={item.title} text={item.text} />
)
})}
</ReactCSSTransitionGroup>
</div>
)
}
}
So i added transitionAppear={true} and used the following css selectors in my scss file:
.pricing-appear {
opacity: 0.01;
}
.pricing-appear.pricing-appear-active {
opacity: 1;
transition: opacity .5s ease-in;
}
The issue was that i wanted to animate on initial mounting of the components.
Related
With React Styled Components, how do I declare the colour based on state collected via useSelector?
I'm using Styled Components in my app. I am collecting state via useSelector using React-Redux, this state returns either a 0 or 1 - 0 telling me the app is currently in light mode, 1 telling me the app is currently in dark mode. How do I edit the style based on the value? I have tried the following... const Logo = styled(motion.h1)` color: ${themeColour ? "white" : "black"}; `; However, this didn't work as the variable exists inside a function. I then tried to style the h1 component directly in the JSX like this... <h1 style={{color: ${themeColour ? "white" : "black"}}}> I got the following error... "./src/components/Nav.js SyntaxError: C:\Users\james\Documents\Web Development\Portfolio\Project 1 - RelaxStation\Code\relax-station\src\components\Nav.js: Unexpected token, expected "," (14:26)" Could someone help me figure this out please? Below is my full code for the component (please note I couldn't indent it correctly when pasting the code in here, but it is correct in Visual Studio Code)... // Libraries import styled from "styled-components"; import { motion } from "framer-motion"; // Redux import { useSelector } from "react-redux"; const Nav = () => { // Get current theme colour code - Gives a 0 for light mode or a 1 for dark mode const themeColour = useSelector((state) => state.theme); return ( <StyledNav> <Logo> Relax<span>Station</span> </Logo> <div className="main"> <h4>MAIN</h4> <ul className="main-links"> <li>Home</li> <li>Artists</li> <li>Albums</li> </ul> </div> <div className="playlists"> <h4>PLAYLISTS</h4> <ul className="playlist-links"> <li>Early morning</li> <li>Studying</li> <li>Driving</li> <li>Ambience</li> </ul> </div> </StyledNav> ); }; const StyledNav = styled(motion.nav)` position: static; width: 10rem; top: 0; left: 0; min-height: 95vh; border: 2px solid rgba(255, 255, 255, 0.125); `; const Logo = styled(motion.h1)` color: ${themeColour ? "white" : "black"}; `; export default Nav; Thanks in advance
You need to pass on the themeColor as a prop to tthe Logo component while render <Logo themeColor={themeColor}> Relax<span>Station</span> </Logo> and then use it with styled component like const Logo = styled(motion.h1)` color: ${props => props.themeColour? "white" : "black"}; `; For the h1 tag your code didn't work because your syntax was incorrect Below is the correct usage <h1 style={{color: themeColour ? "white" : "black"}}>
Add Space Between Row Elements in React BootStrap
I am new to React and react-bootstrap. I have a Row component and would like spacing between the top of the screen and also between the individual row elements. I am currently just editing its CSS but heard that it may be bad to mess with the CSS of frameworks? const Styles = styled.div` .background { background: url(${backgroundImage}) no-repeat fixed bottom; background-size: cover; height: 100vh; position: relative; } #row { position: relative; top: 200px; } `; class Signup extends React.Component { render() { return ( <Styles> <div className="background"> <Container> <Row id="row" className="justify-content-md-center"> <ExtraInfo /> <SignupForm /> </Row> </Container> </div> </Styles> ); } } export default Signup;
Use Form.Group in React-Bootstrap. It adds 15px padding on columns to separate the content of the columns.
I'm not saying this is the best solution, but I have just created a CSS class that sets a margin attribute to 5px and then applied it to the Row element where I need it. This works for me, but I'm welcome to better answers. For space at the top and bottom of the screen, what I have done is create a "spacer" component which I import and use to create space between elements. I know what you mean about messing with the CSS though and this method might be a bit "hacky". Example JSX: <> <p><strong>My Categories</strong></p> { catList.map((cat,i) => ( <Row key={i} className="add-space"> <Col><strong>Category {i+1}</strong></Col> <Col>{cat}</Col> <Col><Button onClick={() => removeCategory(cat)}>Remove</Button></Col> </Row> )) } </> Example CSS: .add-space { margin: 4px; } Example Spacer: const Spacer = props => { return ( <div style={{height:props.height}}></div> ); } export default Spacer; Use: <Spacer height="1rem" />
How to Prevent Parent Component State from Resetting to Original after Closing the Child Component
I have a search form with a lot of search components and a List container component which contains Item component to display the search results. When I click a selected Item, it pops up a Detail component. Right now, everything works fine except when clicking Close button inside the Detail component, the form gets reset and list of items also disappears. The Close button should just close the Detail component so I can select a different item in the list to view. What is the problem in my code? Thanks. App.js class App extends Component { state={ showPopup: false, selectedItem:'', Items:[]}; togglePopup=()=> { this.setState({ showPopup: !this.state.showPopup }); } onItemseSelect=(item)=>{ this.setState({selectedItem:item}); }; render(){ const Items=['aa','bb','cc']; return( <List Items={this.state.Items} onItemSelect={this.onItemSelect} onClick={this.togglePopup} /> {this.state.showPopup ? <Detail item={this.state.selectedItem} closePopup={this.togglePopup.bind(this)} /> : null } ); } } List.js import React from 'react'; import Item from './Item'; const List=({Items,onItemSelect})=>{ const renderedList= Items.map(item=>{ return ( <Item key={item.ID} item={item} onItemSelect={onItemSelect} /> ); }) return <div> {renderedList}</div> } export default List; Item.js import React from 'react'; const Item=({item, onItemSelect})=>{ return <div onClick={()=>onItemSelect(item)} > <div class="content"> <div class="header"> {/*display contents*/} View More </div> </div> }; export default Item; Detail.js import React from 'react'; const Detail=({item,closePopup})=>{ if (!item){ return <div>loading</div> } return ( <div className='popup'> <div className='popup_inner'> <p> {/*contents here*/} </p> <button onClick={()=>closePopup}>close me</button> </div> </div>); }; export default Detail; css code: .popup { position: fixed; width: 100%; height: 100%; top: 0; left: 0; right: 0; bottom: 0; margin: auto; background-color: rgba(0,0,0, 0.5); } .popup_inner { position: absolute; left: 25%; right: 25%; top: 25%; bottom: 25%; margin: auto; background: white; } No error message. The form resets to the original state.
I think the problem is here only, I doubt how your list items are rendered first time only. Items:[] render(){ const Items=['aa','bb','cc']; //static values which are not in use return( <List Items={this.state.Items} //You are using state which is blank onItemSelect={this.onItemSelect} onClick={this.togglePopup} /> ... ) } Complete Running code is like this.
React CSS Transition Group, not working?
Apologies for the simplicity of this problem, but I am new to React and trying to implement a simple CSSTransitionGroup to hide/show an element, but with a fade, slide, etc. The documentation seems very straightforward, but for some reason the following code will not work for me. While the .box toggles between being there or not, I do not see any of the CSS transitions in place on enter and on leave. class Demo extends React.Component{ constructor(props) { super(props); this.state = { visible: false }; this.handleClick = this.handleClick.bind(this) } handleClick() { this.setState({ visible: ! this.state.visible }); } render() { return <div> <button onClick={this.handleClick}>{this.state.visible ? 'Slide up' : 'Slide down'}</button> <ReactCSSTransitionGroup transitionName="example"> { this.state.visible ? <div className='box' /> : null } </ReactCSSTransitionGroup> </div> } } .box { width: 200px; height: 100px; background: green; margin-top: 10px; } .example-enter { height: 0px; } .example-enter.example-enter-active { height: 100px; -webkit-transition: height .3s ease; } .example-leave.example-leave-active { height: 0px; -webkit-transition: height .3s ease; } I must be doing something wrong, as I can see this basic demo working in other online examples, but cannot replicate myself. Please let me know how to get my CSS transitions. Thx internet
Did you check the console for error output? Because when I run your code I get this: "Warning: Failed propType: transitionEnterTimeout wasn't supplied to ReactCSSTransitionGroup: this can cause unreliable animations and won't be supported in a future version of React. See xxx for more information. Check the render method of Demo." "Warning: Failed propType: transitionLeaveTimeout wasn't supplied to ReactCSSTransitionGroup: this can cause unreliable animations and won't be supported in a future version of React. See xxx for more information. Check the render method of Demo." Adding the two missing props and it works fine. class Demo extends React.Component{ constructor(props) { super(props); this.state = { visible: false }; this.handleClick = this.handleClick.bind(this) } handleClick() { this.setState({ visible: ! this.state.visible }); } render() { return <div> <button onClick={this.handleClick}>{this.state.visible ? 'Slide up' : 'Slide down'}</button> <ReactCSSTransitionGroup transitionName="example" transitionEnterTimeout={300} transitionLeaveTimeout={300} > { this.state.visible ? <div className='box' /> : null } </ReactCSSTransitionGroup> </div> } } transitionEnterTimeout and transitionLeaveTimeout both take a number representing the duration of the transition in milliseconds, hence "300" for a .3 second transition, which matches the CSS transitions you've specified.
Inline CSS styles in React: how to implement a:hover?
I quite like the inline CSS pattern in React and decided to use it. However, you can't use the :hover and similar selectors. So what's the best way to implement highlight-on-hover while using inline CSS styles? One suggestion from #reactjs is to have a Clickable component and use it like this: <Clickable> <Link /> </Clickable> The Clickable has a hovered state and passes it as props to the Link. However, the Clickable (the way I implemented it) wraps the Link in a div so that it can set onMouseEnter and onMouseLeave to it. This makes things a bit complicated though (e.g. span wrapped in a div behaves differently than span). Is there a simpler way?
I think onMouseEnter and onMouseLeave are the ways to go, but I don't see the need for an additional wrapper component. Here is how I implemented it: var Link = React.createClass({ getInitialState: function(){ return {hover: false} }, toggleHover: function(){ this.setState({hover: !this.state.hover}) }, render: function() { var linkStyle; if (this.state.hover) { linkStyle = {backgroundColor: 'red'} } else { linkStyle = {backgroundColor: 'blue'} } return( <div> <a style={linkStyle} onMouseEnter={this.toggleHover} onMouseLeave={this.toggleHover}>Link</a> </div> ) } You can then use the state of hover (true/false) to change the style of the link.
Late to party but come with solution. You can use "&" to defines styles for hover nth Child etc: day: { display: "flex", flex: "1", justifyContent: "center", alignItems: "center", width: "50px", height: "50px", transition: "all 0.2s", borderLeft: "solid 1px #cccccc", "&:hover": { background: "#efefef" }, "&:last-child": { borderRight: "solid 1px #cccccc" } },
I'm in the same situation. Really like the pattern of keeping the styling in the components but the hover states seems like the last hurdle. What I did was writing a mixin that you can add to your component that needs hover states. This mixin will add a new hovered property to the state of your component. It will be set to true if the user hovers over the main DOM node of the component and sets it back to false if the users leaves the element. Now in your component render function you can do something like: <button style={m( this.styles.container, this.state.hovered && this.styles.hover, )}>{this.props.children}</button> Now each time the state of the hovered state changes the component will rerender. I've also create a sandbox repo for this that I use to test some of these patterns myself. Check it out if you want to see an example of my implementation. https://github.com/Sitebase/cssinjs/tree/feature-interaction-mixin
You can use Radium - it is an open source tool for inline styles with ReactJS. It adds exactly the selectors you need. Very popular, check it out - Radium on npm
Here's my solution using React Hooks. It combines the spread operator and the ternary operator. style.js export default { normal:{ background: 'purple', color: '#ffffff' }, hover: { background: 'red' } } Button.js import React, {useState} from 'react'; import style from './style.js' function Button(){ const [hover, setHover] = useState(false); return( <button onMouseEnter={()=>{ setHover(true); }} onMouseLeave={()=>{ setHover(false); }} style={{ ...style.normal, ...(hover ? style.hover : null) }}> MyButtonText </button> ) }
Full CSS support is exactly the reason this huge amount of CSSinJS libraries, to do this efficiently, you need to generate actual CSS, not inline styles. Also inline styles are much slower in react in a bigger system. Disclaimer - I maintain JSS.
Made Style It -- in part -- because of this reason (others being disagreements with implementation of other libs / syntax and inline stylings lack of support for prefixing property values). Believe we should be able to simply write CSS in JavaScript and have fully self contained components HTML-CSS-JS. With ES5 / ES6 template strings we now can and it can be pretty too! :) npm install style-it --save Functional Syntax (JSFIDDLE) import React from 'react'; import Style from 'style-it'; class Intro extends React.Component { render() { return Style.it(` .intro:hover { color: red; } `, <p className="intro">CSS-in-JS made simple -- just Style It.</p> ); } } export default Intro; JSX Syntax (JSFIDDLE) import React from 'react'; import Style from 'style-it'; class Intro extends React.Component { render() { return ( <Style> {` .intro:hover { color: red; } `} <p className="intro">CSS-in-JS made simple -- just Style It.</p> </Style> ); } } export default Intro;
I found a clean way to do this with a wrapper around useState, which I call useHover. No additional libraries/frameworks needed. const App = () => { const hover = useHover({backgroundColor: "LightBlue"}) return <p {...hover}>Hover me!</p> } Code for the wrapper: function useHover(styleOnHover: CSSProperties, styleOnNotHover: CSSProperties = {}) { const [style, setStyle] = React.useState(styleOnNotHover); const onMouseEnter = () => setStyle(styleOnHover) const onMouseLeave = () => setStyle(styleOnNotHover) return {style, onMouseEnter, onMouseLeave} } Note that useHover has an optional second parameter for a style when the component is not hovered. Try it out here
Heres is another option using CSS variables . This requires a css hover definition ahead of time so I guess its not pure inline, but is very little code and flexible. css (setup a hover state) : .p:hover:{ color:var(--hover-color) !important, opacity:var(--hover-opacity) } react: <p style={{'color':'red','--hover-color':'blue','--hover-opacity':0.5}}>
Adding on to Jonathan's answer, here are the events to cover the focus and active states, and a using onMouseOver instead of onMouseEnter since the latter will not bubble if you have any child elements within the target the event is being applied to. var Link = React.createClass({ getInitialState: function(){ return {hover: false, active: false, focus: false} }, toggleHover: function(){ this.setState({hover: !this.state.hover}) }, toggleActive: function(){ this.setState({active: !this.state.active}) }, toggleFocus: function(){ this.setState({focus: !this.state.focus}) }, render: function() { var linkStyle; if (this.state.hover) { linkStyle = {backgroundColor: 'red'} } else if (this.state.active) { linkStyle = {backgroundColor: 'blue'} } else if (this.state.focus) { linkStyle = {backgroundColor: 'purple'} } return( <div> <a style={linkStyle} onMouseOver={this.toggleHover} onMouseOut={this.toggleHover} onMouseUp={this.toggleActive} onMouseDown={this.toggleActive} onFocus={this.toggleFocus}> Link </a> </div> ) }
onMouseEnter={(e) => { e.target.style.backgroundColor = '#e13570'; e.target.style.border = '2px solid rgb(31, 0, 69)'; e.target.style.boxShadow = '-2px 0px 7px 2px #e13570'; }} onMouseLeave={(e) => { e.target.style.backgroundColor = 'rgb(31, 0, 69)'; e.target.style.border = '2px solid #593676'; e.target.style.boxShadow = '-2px 0px 7px 2px #e13570'; }} Set default properties in the style or class then call onMouseLeave() and onMouseEnter() to create a hover functionality.
Checkout Typestyle if you are using React with Typescript. Below is a sample code for :hover import {style} from "typestyle"; /** convert a style object to a CSS class name */ const niceColors = style({ transition: 'color .2s', color: 'blue', $nest: { '&:hover': { color: 'red' } } }); <h1 className={niceColors}>Hello world</h1>
In regards to styled-components and react-router v4 you can do this: import {NavLink} from 'react-router-dom' const Link = styled(NavLink)` background: blue; &:hover { color: white; } ` ... <Clickable><Link to="/somewhere">somewhere</Link></Clickable>
This can be a nice hack for having inline style inside a react component (and also using :hover CSS function): ... <style> {`.galleryThumbnail.selected:hover{outline:2px solid #00c6af}`} </style> ...
The simple way is using ternary operator var Link = React.createClass({ getInitialState: function(){ return {hover: false} }, toggleHover: function(){ this.setState({hover: !this.state.hover}) }, render: function() { var linkStyle; if (this.state.hover) { linkStyle = {backgroundColor: 'red'} } else { linkStyle = {backgroundColor: 'blue'} } return( <div> <a style={this.state.hover ? {"backgroundColor": 'red'}: {"backgroundColor": 'blue'}} onMouseEnter={this.toggleHover} onMouseLeave={this.toggleHover}>Link</a> </div> ) }
You can use css modules as an alternative, and additionally react-css-modules for class name mapping. That way you can import your styles as follows and use normal css scoped locally to your components: import React from 'react'; import CSSModules from 'react-css-modules'; import styles from './table.css'; class Table extends React.Component { render () { return <div styleName='table'> <div styleName='row'> <div styleName='cell'>A0</div> <div styleName='cell'>B0</div> </div> </div>; } } export default CSSModules(Table, styles); Here is a webpack css modules example
onMouseOver and onMouseLeave with setState at first seemed like a bit of overhead to me - but as this is how react works, it seems the easiest and cleanest solution to me. rendering a theming css serverside for example, is also a good solution and keeps the react components more clean. if you dont have to append dynamic styles to elements ( for example for a theming ) you should not use inline styles at all but use css classes instead. this is a traditional html/css rule to keep html / JSX clean and simple.
This can be easily achived with material-ui makeStyles invocation: import { makeStyles } from '#material-ui/core/styles'; makeStyles({ root: { /* … */ '&:hover': { /* … */ } }, });
This is a universal wrapper for hover written in typescript. The component will apply style passed via props 'hoverStyle' on hover event. import React, { useState } from 'react'; export const Hover: React.FC<{ style?: React.CSSProperties; hoverStyle: React.CSSProperties; }> = ({ style = {}, hoverStyle, children }) => { const [isHovered, setHovered] = useState(false); const calculatedStyle = { ...style, ...(isHovered ? hoverStyle : {}) }; return ( <div style={calculatedStyle} onMouseEnter={() => setHovered(true)} onMouseLeave={() => setHovered(false)} > {children} </div> ); };
I did something similar to this, but I do not use material-ui or makeStyles. I added the hover as a condition in my css in a style object: const styles = { hoverStyle: { color: 'grey', '&:hover': { color: 'blue !important' }, } }; var NavBar = (props) => { const menuOptions = ['home', 'blog', 'projects', 'about']; return ( <div> <div> {menuOptions.map((page) => <div style={styles.hoverStyle} key={page}>{page}</div> )} </div> </div> ); }; This worked for me.
You can just create an abstract hovering class e.g. for the color. .hoverClassColor:hover { color:var(--hover-color) !important; } Then for all Elements you wanna changes the color to red on hovering: render() { return <a className={'hoverClassColor'} style={{'--hover-color':'red'}}>Test</a> } For me its like inline, cause the classes are abstract and can be reused for all of your elements you wanna implement a color hovering.
I use this trick, a mix between inline-style and css: //inline-style: const button = { fontSize: "2em", }; return ( <div style={button} data-hover="button"> <style>{`[data-hover="button"]:hover { font-size: 2.1em !important; }`}</style> {this.props.text} </div> );
Easiest way 2022: useRef + inline onMouseOver/onMouseOut example: var bottomAtag = useRef(null) ...then inside return ( <a ref={bottomAtag} onMouseOver={() => bottomAtag.current.style.color='#0F0'} ...></a>
With a using of the hooks: const useFade = () => { const [ fade, setFade ] = useState(false); const onMouseEnter = () => { setFade(true); }; const onMouseLeave = () => { setFade(false); }; const fadeStyle = !fade ? { opacity: 1, transition: 'all .2s ease-in-out', } : { opacity: .5, transition: 'all .2s ease-in-out', }; return { fadeStyle, onMouseEnter, onMouseLeave }; }; const ListItem = ({ style }) => { const { fadeStyle, ...fadeProps } = useFade(); return ( <Paper style={{...fadeStyle, ...style}} {...fadeProps} > {...} </Paper> ); };
<Hoverable hoverStyle={styles.linkHover}> <a href="https://example.com" style={styles.link}> Go </a> </Hoverable> Where Hoverable is defined as: function Hoverable(props) { const [hover, setHover] = useState(false); const child = Children.only(props.children); const onHoverChange = useCallback( e => { const name = e.type === "mouseenter" ? "onMouseEnter" : "onMouseLeave"; setHover(!hover); if (child.props[name]) { child.props[name](e); } }, [setHover, hover, child] ); return React.cloneElement(child, { onMouseEnter: onHoverChange, onMouseLeave: onHoverChange, style: Object.assign({}, child.props.style, hover ? props.hoverStyle : {}) }); }
I use a pretty hack-ish solution for this in one of my recent applications that works for my purposes, and I find it quicker than writing custom hover settings functions in vanilla js (though, I recognize, maybe not a best practice in most environments..) So, in case you're still interested, here goes. I create a parent element just for the sake of holding the inline javascript styles, then a child with a className or id that my css stylesheet will latch onto and write the hover style in my dedicated css file. This works because the more granular child element receives the inline js styles via inheritance, but has its hover styles overridden by the css file. So basically, my actual css file exists for the sole purpose of holding hover effects, nothing else. This makes it pretty concise and easy to manage, and allows me to do the heavy-lifting in my in-line React component styles. Here's an example: const styles = { container: { height: '3em', backgroundColor: 'white', display: 'flex', flexDirection: 'row', alignItems: 'stretch', justifyContent: 'flex-start', borderBottom: '1px solid gainsboro', }, parent: { display: 'flex', flex: 1, flexDirection: 'row', alignItems: 'stretch', justifyContent: 'flex-start', color: 'darkgrey', }, child: { width: '6em', textAlign: 'center', verticalAlign: 'middle', lineHeight: '3em', }, }; var NavBar = (props) => { const menuOptions = ['home', 'blog', 'projects', 'about']; return ( <div style={styles.container}> <div style={styles.parent}> {menuOptions.map((page) => <div className={'navBarOption'} style={styles.child} key={page}>{page}</div> )} </div> </div> ); }; ReactDOM.render( <NavBar/>, document.getElementById('app') ); .navBarOption:hover { color: black; } <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> Notice that the "child" inline style does not have a "color" property set. If it did, this would not work because the inline style would take precedence over my stylesheet.
I'm not 100% sure if this is the answer, but its the trick i use to simulate the CSS :hover effect with colours and images inline. `This works best with an image` class TestHover extends React.PureComponent { render() { const landingImage = { "backgroundImage": "url(https://i.dailymail.co.uk/i/pix/2015/09/01/18/2BE1E88B00000578-3218613-image-m-5_1441127035222.jpg)", "BackgroundColor": "Red", `this can be any color` "minHeight": "100%", "backgroundAttachment": "fixed", "backgroundPosition": "center", "backgroundRepeat": "no-repeat", "backgroundSize": "cover", "opacity": "0.8", `the hove trick is here in the opcaity slightly see through gives the effect when the background color changes` } return ( <aside className="menu"> <div className="menu-item"> <div style={landingImage}>SOME TEXT</div> </div> </aside> ); } } ReactDOM.render( <TestHover />, document.getElementById("root") ); CSS: .menu { top: 2.70em; bottom: 0px; width: 100%; position: absolute; } .menu-item { cursor: pointer; height: 100%; font-size: 2em; line-height: 1.3em; color: #000; font-family: "Poppins"; font-style: italic; font-weight: 800; text-align: center; display: flex; flex-direction: column; justify-content: center; } Before hover .menu-item:nth-child(1) { color: white; background-color: #001b37; } On hover .menu-item:nth-child(1):hover { color: green; background-color: white; } Example: https://codepen.io/roryfn/pen/dxyYqj?editors=0011
Here is how I do it with hooks in functional components. With onMouseEnter/Leave, im setting the color as state directly and consume it in the style prop of element (instead of setting hover state and using ternaries to change the state as shown in previous answers). function App() { const [col, setCol] = React.useState('white'); return ( <div className="App"> <button style={{background: `${col}`}} onMouseEnter={() => setCol("red")} onMouseLeave={() => setCol("white")} > Red </button> </div> ); } ReactDOM.render(<App/>, document.getElementById('root')) <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js" integrity="sha256-3vo65ZXn5pfsCfGM5H55X+SmwJHBlyNHPwRmWAPgJnM=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js" integrity="sha256-qVsF1ftL3vUq8RFOLwPnKimXOLo72xguDliIxeffHRc=" crossorigin="anonymous"></script> <div id='root'></div>
This solution does use stylesheets. However, If your application uses an index.css - i.e. has a stylesheet that gets imported into your top-level component, you could just write the following code in there .hoverEffect:hover { //add some hover styles } Then in your React component, just add the className "hoverEffect" to apply the hover effect "inline". If the hover state is being passed down as a prop, and you only want it to be applied to the child component, remove the :hover in your index.css, and do this instead. function Link(props) { return ( <a className={props.isHovered ? "hoverEffect" : ""}>Hover me<a/> ) }
Directly use tag in your component like this: <Wrapper> <style> .custom-class{ // CSS here } .custom-class:hover{ //query selector } </style> <div className="custom-class"></div> </Wrapper>