【问题标题】:React image onClick to display in another component?反应图像onClick以显示在另一个组件中?
【发布时间】:2017-09-21 12:51:18
【问题描述】:

是否可以单击组件中的一个图像并让该 onClick 在另一个组件中显示相同的图像和信息?我的脑子一片空白,我一直在寻找答案。

我创建了一个显示多个图像的组件。我还创建了 3 个单独的组件,我想根据在第一个组件中单击的图像显示图像和一些文本。我想过为每个图像路由和创建组件,但觉得这种方法效率不高……

提前谢谢你!

这是主要组件:

import React from 'react';
import './app.css';
import ImageSection from './image_section';
import PaneOne from './pane_1';
import PaneTwo from './pane_2';
import PaneThree from './pane_3';


const App = () => (
    <div className="main_section">
        <div className="top_pane_area">
            <PaneOne />
            <PaneTwo />
            <PaneThree />
        </div>
        <div className="bottom_image_area">
            <ImageSection />
        </div>
    </div>
);

export default App;

这是我希望显示图像的所有三个组件(目前;所有三个组件都是相同的):

import React, { Component } from 'react';
import './app.css';


class PaneOne extends Component {
    render () {
        return (
            <div className="panes">

            </div>
        )
    }
}

export default PaneOne;

这是带有图像的组件(目前):

import React, { Component } from 'react';
import './app.css';
import one from './imgs/one.jpg';
import two from './imgs/two.jpg';
import three from './imgs/three.jpg';
import four from './imgs/four.jpg';
import five from './imgs/five.jpg';
import six from './imgs/six.jpg';


class ImageSection extends Component {
    render() {
        return(
            <div>
                <div>
                    <img className="prof_pic" src={one} />
                    <img className="prof_pic" src={two} />
                    <img className="prof_pic" src={three} />
                    <img className="prof_pic" src={four} />
                    <img className="prof_pic" src={five} />
                    <img className="prof_pic" src={six} />
                </div>
            </div>
        )
    }
}

export default ImageSection;

【问题讨论】:

  • 是的,它绝对可行。你能发布一些代码吗?
  • 对不起!忘记添加代码了。现已添加 :)

标签: javascript image reactjs


【解决方案1】:

使父组件发送一个函数,该函数将由子组件(带有图像的组件)的 props 接收,并通过单击图像将有关该函数的信息发送回父组件。这将允许孩子与父母交流,还允许您为自定义逻辑发送任何附加参数。然后父函数可以设置状态或任何你想要的,这取决于从图像上的点击事件传递的信息。

class App extends Component{
    constructor(props){
        super(props);

        // set the state to handle which of the panes displays the information you want

        this.image_clicked = this.image_clicked.bind(this);
    }

    render(){
        return (
            <div className="main_section">
                <div className="top_pane_area">
                    <PaneOne />
                    <PaneTwo />
                    <PaneThree />
                </div>
                <div className="bottom_image_area">
                    <ImageSection image_clicked={this.image_clicked}/>
                </div>
            </div>
        )
    }

    image_clicked(source){
        // Based on the source or any additional parameter you send do any re-render logic
        // example:
        if (source == 'image1.png'){
            this.setState({pane1 : source});
        }
    }
}

class ImageSection extends Component {
    render() {
        let image_clicked = this.props.image_clicked;

        return(
            <div>
                <div>
                    <img className="prof_pic" src={one} onClick={() => image_clicked(one)}/>
                </div>
            </div>
        )
    }
}

【讨论】:

  • 对不起,我在设置原始状态时有点迷路了。您说过要设置状态以处理将显示信息的任何窗格。我可以设置 this.state = {PaneOne = {image: '"", name: "", description: ""}}
  • 这可能是一个选择。您的状态中还可以有三个不同的对象,每个窗格一个,然后根据单击的图像,您可以设置该特定窗格的状态。您还可以向 image_clicked 函数发送一个附加参数以获取窗格编号,以便您知道哪个窗格对象将被更改。
  • 我要在 App 组件中设置状态吗?我已经这样设置了 App 组件:this.state = { PaneOne: { profile_img: '', name: '', description: '' }, PaneTwo: { profile_img: '', name: '', description: '' }, PaneThree: { profile_img: '', name: '', description: '' } } this.image_clicked = this.image_clicked.bind(this); }
  • 我会在每个窗格组件中做同样的事情吗?用一个对象而不是三个对象设置状态?对困惑感到抱歉。仍然是新的反应......
  • 感谢您的帮助!我能够根据您给我的帮助创建一个完整的工作应用程序!我确实有一个问题。我这样做是为了在单击图像时更改状态并放入pane1。同时,pane2 将包含来自 pane1 的状态,而 pane3 将包含来自 pane2 的状态(转换)。有没有办法在该过渡中包含动画?现在只是一个快速的改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2018-06-30
  • 2023-01-19
  • 1970-01-01
  • 2017-06-12
  • 2016-04-11
相关资源
最近更新 更多