简体   繁体   English

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

[英]How do you use multiple react components on the same element?

I would like to hover a div, get the image inside that div, show it and then make the image follow my cursor. 我想将鼠标悬停在div上,将图像显示在该div内,显示它,然后使图像跟随我的光标。 The hover component is working but then I can't make the follow cursor component work. 悬停组件正在工作,但是我无法使跟随光标组件工作。 How would you go about doing this ? 您将如何去做?

Component number 1 : 组件编号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;

Component 2 : 组成部分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;

and then the parent component. 然后是父组件。

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;

I have no idea what to do with the other component, can you even render childs inside a parent like this ? 我不知道该如何处理其他组件,您是否可以像这样在父对象中渲染子对象?

I would like pass the props from FollowMouse as style attributes of my img element. 我想将FollowMouse的道具作为img元素的样式属性传递。

I'm not 100% sure I follow your question, and I don't know what you're expecting this.props.children to do when invoked as a function, but if you want to add props to a child element you can do so via React.cloneElement : 我不是100%肯定会遵循您的问题,也不知道您期望this.props.children作为函数调用时会做什么,但是如果您想将props添加到child元素中,则可以所以通过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>
)

Given: 鉴于:

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

The child component will get the additional props, the equivalent of: 子组件将获得其他道具,等效于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何访问多个组件中的反应挂钩? 我的反应钩子做错了什么? - How do you access react hooks in multiple components? What am I doing wrong with my react hook? 如何在Redux表单中为同一父级的多个表单子级组件管理多个#onSubmits? - How do you manage multiple #onSubmits for multiple form children components of the same parent in Redux Form? 你如何用反应钩子做点符号反应组件? - how do you do dot notation react components with react hooks? 如何在反应中在同一个 html 元素上使用多个 ref 变量? - How to use multiple ref variables on same html element in react? 当您在本机反应中有多个重复组件时该怎么办 - what to do when you have multiple repetitive components in react native 在反应中使用多个组件 - Use multiple components in react 如何在Angular 2中使用同一文件中的组件? - How can you use components from the same file in Angular 2? 您如何嵌套React组件/模块以分离UI? - How do you nest React components/modules to separate the UI? 你如何在 React 中使用子组件实现“全部检查”? - How do you implement “check all” in React with child components? 你什么时候在反应中使用类组件,因为 useState 现在在功能组件中可用 - When do you use class components in react since useState is now available in Functional components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM