ReactJS set display='inline' onMouseOver

ReactJS set display='inline' onMouseOver



I would like to set display property of a child element to inline, when there is a mouseOver or mouseEnter event in this React component.



I can see that the state is being set, but it does not change for the display property.


export class EditablePageTitle extends React.Component
state =
showEditIcon: 'none'
;

showEditIcon()
console.log('showEditIcon 1 ', this.state.showEditIcon);
this.setState( showEditIcon: 'inline' );
console.log('showEditIcon 2 ', this.state.showEditIcon);


render()
return (
<FlexColumn
style=
alignItems: 'baseline',
paddingBottom: s[3]

>
<div id="page-title" onMouseEnter=() => this.showEditIcon()>
<Input
...this.props
type=this.props.type
defaultValue=this.props.defaultValue
disabled="disabled"
onChange=this.props.onChange
/>
<i
className="fa fa-pencil-alt"
style=
paddingRight: s[3],
color: '#FFFFFF',
fontSize: s[3],
display: this.state.showEditIcon

/>
console.log('this.state.showEditIcon ', this.state.showEditIcon)
</div>
<Label>this.props.label</Label>
</FlexColumn>
);







Try <div id="page-title" onMouseEnter=this.showEditIcon>

– Alex Sandiiarov
Sep 7 '18 at 21:41




2 Answers
2



This is possibly a conflict with how Font Awesome and React handle rendering.



If you are using React we recommend the react-fontawesome package or Web Fonts with CSS.



https://fontawesome.com/how-to-use/on-the-web/using-with/react



However, you may just want to try wrapping your icon with a span and apply the display property there.


span


display


<span style= display: this.state.showEditIcon >
<i className="fa fa-pencil-alt"
style=
paddingRight: s[3],
color: '#FFFFFF',
fontSize: s[3]

/>
</span>






Thanks, I just decided to wrap it in a span, and I'll revisit later and think about installing the other stuff

– ss_matches
Sep 7 '18 at 22:22



Call the showEditIcon method like the following and also you should bind this:


showEditIcon


this


export class EditablePageTitle extends React.Component
constructor(props)
super(props);
this.state =
showEditIcon: 'none'
;

this.showEditIcon = this.showEditIcon.bind(this);


showEditIcon()
console.log('showEditIcon 1 ', this.state.showEditIcon);
this.setState( showEditIcon: 'inline' );
console.log('showEditIcon 2 ', this.state.showEditIcon);


render()
return (
<FlexColumn
style=
alignItems: 'baseline',
paddingBottom: s[3]

>
<div id="page-title" onMouseEnter=this.showEditIcon>
<Input
...this.props
type=this.props.type
defaultValue=this.props.defaultValue
disabled="disabled"
onChange=this.props.onChange
/>
<i
className="fa fa-pencil-alt"
style=
paddingRight: s[3],
color: '#FFFFFF',
fontSize: s[3],
display: this.state.showEditIcon

/>
console.log('this.state.showEditIcon ', this.state.showEditIcon)
</div>
<Label>this.props.label</Label>
</FlexColumn>
);







Unfortunately, I'm still not seeing the icon's display property set to inline

– ss_matches
Sep 7 '18 at 21:56






@ss_matches your error must be elsewhere then. Without additional context it's hard to determine. I tried to recreate your code as best as possible here: codesandbox.io/s/1r4yl71r44 and it works as expected (also: I opted to use an arrow function instead of bind())

– garetmckinley
Sep 7 '18 at 22:12


bind()



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)