如何在同一元素上使用多个反应组件?

时间:2019-09-07 05:18:44

标签: javascript reactjs

我想将鼠标悬停在div上,将其显示在该div中,然后显示该图像,然后使该图像跟随我的光标。悬停组件正在工作,但是我无法使跟随光标组件工作。您将如何去做?

组件编号1:

import React from 'react';

class FollowMouse extends React.Component {

    state = {
        xPos: 0,
        yPos: 0
    };

    onMouseMove(e) {
        this.setState({
            xPos: e.screenX,
            yPos: e.screenY
        });
    }

    render() {
        return (
            <div
                onMouseMove={this.onMouseMove.bind(this)}
                className="img-ctn"
            >
                {this.props.children(this.state.xPos, this.state.yPos)}
            </div >
        );
    }
}
export default FollowMouse;

组件2:

import React from 'react';

class HoverProject extends React.Component {

    state = {
        isHovered: false,
    };

    onMouseEnter() {
        this.setState({ isHovered: true });
    }
    onMouseLeave() {
        this.setState({ isHovered: false });
    }

    render() {
        return (
            <div
                onMouseEnter={this.onMouseEnter.bind(this)}
                onMouseLeave={this.onMouseLeave.bind(this)}
                className="project-item"
            >
                {this.props.children(this.state.isHovered)}
            </div >
        );
    }
}
export default HoverProject;

,然后是父组件。

import React from 'react';

// modules
import HoverProject from '../modules/HoverProject';
import FollowMouse from '../modules/FollowMouse';


import VLEC from '../images/vlec.png';

class ProjectList extends React.Component {

    constructor(props) {
        super(props);

        this.sels = {
            state: 'active'
        };
    };

    render() {
        return (
            <div className="project-list module">

                <div className="sectionTitle">Project I've worked on</div>

                {this.props.data.map((res, i) => (
                    <HoverProject key={i}>
                        {
                            isHovered =>
                                <div className="inner-ctn">
                                    {/* <FollowMouse /> */}
                                    <img className={"project-image " + (isHovered ? this.sels.state : "")} src={VLEC} alt="VLEC" />
                                    <div className="header">
                                        <div className="number">0{res.id + 1}</div>
                                        <div className="name">{res.nomProjet}</div>
                                    </div>
                                    <div className="item-ctn">
                                        <div className="categ">{res.categProjet}</div>
                                        <div className="roles">{res.roles}</div>
                                        <div className="date">{res.date}</div>
                                    </div>

                                </div>
                        }
                    </HoverProject>
                ))}
            </div>
        );
    }
}
export default ProjectList;

我不知道该如何处理其他组件,您是否可以像这样在父对象中渲染子对象?

我想将FollowMouse的道具作为img元素的样式属性传递。

1 个答案:

答案 0 :(得分:0)

我不确定是否100%遵循您的问题,并且不知道您希望this.props.children作为函数调用时会做什么,但是如果您想向子元素添加道具您可以通过React.cloneElement进行操作:

const ParentComponent ({children}) => (
  <div>
    {React.cloneElement(
      React.Children.only(children), // clone the only child...
      { style: { left: xPos, top: yPos} } // ...and add new props
    )}
  </div>
)

给出:

<ParentComponent>
  <div>Wookies and Hats</div>
</ParentComponent>

子组件将获得其他道具,等效于:

<ParentComponent>
  <div style={{left: xPos, top: yPos}}>Wookies and Hats</div>
</ParentComponent>