0

Doing a simple tic-tac-toe game. I am trying to get a single cell highlighted on mouse-enter and than back to normal on mouse-leave - using state and inline styling to accomplish that. State changes fine on mouse-enter (so is the styling) however it never sets back to normal on mouse-leave. What am I missing here?

import React, { useState } from 'react';

function Cell(props) {

    let defaultStyle = {backgroundColor: 'none'}
    let selectedStyle = {backgroundColor: 'blue'}


    const [style, setStyle] = useState(defaultStyle)

    const sendData = () =>{
        props.clicker(props.cords)
    }

    const set = () =>{
        setStyle(selectedStyle);

    }

    const unset = () =>{
        setStyle(defaultStyle);
    }

    return (
        <td onClick = {sendData} onMouseEnter= {set} onMouseLeave = {unset} style ={style}>{props.content}</td>
    )
}

export default Cell
Shai
  • 27
  • 1
  • 4

3 Answers3

0

Switch backgroundColor: 'none' with backgroundColor: 'transparent'

function Cell(props) {

    let defaultStyle = {backgroundColor: 'transparent'}
    let selectedStyle = {backgroundColor: 'blue'}


    const [style, setStyle] = React.useState(defaultStyle)

    const sendData = () =>{
        props.clicker(props.cords)
    }

    const set = () =>{
        setStyle(selectedStyle);

    }

    const unset = () =>{
        setStyle(defaultStyle);
    }

    return (
        <td onClick={sendData} onMouseEnter={set} onMouseLeave={unset} style={style}>{props.content}</td>
    )
}

ReactDOM.render(<Cell clicker={() => {}} cords={() => {}} content='my content' />, document.querySelector('#root'))
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<div id="root"></div>
Pablo Darde
  • 5,844
  • 10
  • 37
  • 55
0

none is not a valid background-color, so React skips it and keeps using the previous style of blue. Use something else, like the empty string:

function Cell(props) {

    let defaultStyle = {backgroundColor: ''}
    let selectedStyle = {backgroundColor: 'blue'}


    const [style, setStyle] = React.useState(defaultStyle)

    const sendData = () =>{
        props.clicker(props.cords)
    }

    const set = () =>{
        setStyle(selectedStyle);

    }

    const unset = () =>{
        setStyle(defaultStyle);
    }

    return (
        <div onClick = {sendData} onMouseEnter= {set} onMouseLeave = {unset} style ={style}>{props.content}</div>
    )
}

ReactDOM.render(<Cell content='foobar' />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

First of all, don't put a space between '=' and brackets, here is the corrected line :

<td onClick={sendData} onMouseEnter={set} onMouseLeave={unset} style={style}>{props.content}</td>

Second, change {backgroundColor: 'none'} to {backgroundColor: 'transparent'}

Hope you can get your code work :)

YanouHD
  • 1,037
  • 8
  • 15