亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么這個應用程序代碼只能在右側工作,否則 ReactDOM.findDOMNode

為什么這個應用程序代碼只能在右側工作,否則 ReactDOM.findDOMNode

qq_笑_17 2023-03-03 15:40:50
我學習 Reactjs 和 Javascript 并且對這個小文件Codesandbox Hover effect REACT (forked)有疑問。當鼠標移到應用程序上方時,該應用程序會在帶有文本的疊加層中滑動,而當鼠標離開時,疊加層會隱藏。為什么只有當鼠標從右側進入和存在時才經常起作用?試試看你會發現有點奇怪..我看到這個錯誤:我檢查了 DOM,className我認為這是正確的,所以也許是我使用了一些錯誤的版本導入組合,或者這是一個時間問題?我做了一個電影剪輯
查看完整描述

1 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

當 時handleMouseLeave,事件目標將是子組件,如 (p, h2, h3,...)。所以,ReactDOM.findDOMNode(e.target).querySelector找不到外在的喜歡.box-content。


我建議使用簡單的解決方案useState。


import React from "react";

import axios from "axios";


class App extends React.Component {

    state = {

        posts: [],

        hoverIndex: null,

    };


    componentDidMount() {

        axios

            .get(`https://jsonplaceholder.typicode.com/posts`)

            .then((res) => {

                this.setState({ posts: res.data });

            })

            .then((error) => {

                console.log(error);

            });

    }


    handleMouseEnter = (e) => {

        if (e.target.id !== null) {

            this.setState({hoverIndex: e.target.id});

        }

    };


    handleMouseLeave = (e) => {

        this.setState({hoverIndex: null});

    };


    render() {

        const { posts } = this.state;


        return (

            <div>

                {posts.map((e, index) => (

                    <div

                        onMouseEnter={this.handleMouseEnter}

                        onMouseLeave={this.handleMouseLeave}

                        className={"box-container"}

                        id={index}

                        key={e.id}

                    >

                        <h3>HOVER ME</h3>

                        <div

                            className={"box-content " + (this.state.hoverIndex === index.toString() ? 'hovered' : "")}

                        >

                            <h2>{e.title}</h2>

                            <p>{e.body}</p>

                        </div>

                    </div>

                ))}

            </div>

        );

    }

}


export default App;



查看完整回答
反對 回復 2023-03-03
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號