简体   繁体   English

如何将(this)道具传递给React中的兄弟组件

[英]How to pass (this) props to sibling components in React

I'm mapping a list of data in which each listing has a button which opens a modal to display a full listing's data. 我正在映射一个数据列表,其中每个列表都有一个按钮,该按钮打开一个模式以显示完整列表的数据。 Except instead of mapping each entry with a button containing its own modal component mapped per each listing, I'd like to have each button trigger an outside modal component. 除了要用一个包含每个列表所映射的自身模态组件的按钮映射每个条目之外,我希望每个按钮都触发一个外部模态组件。

handleTaskModal = () =>{

    <TaskModal {...state} />
  }


  render() {
    const { tasks } = this.state;

    return (
      <div>
        <Container color="light">

          <Row className="flex-row">
            {tasks.map(({ name, image, description }) => (
                <Col key={name}
                  xs="11"
                  sm="6"
                  lg="4"
                  className="mx-auto my-sm-2 my-lg-1"
                  >
                    <Card className="task-card border-0 rounded shadow-sm my-3">
                      <CardImg
                        top
                        width="100%"
                        src={image}
                        style={{
                          width: "100%",
                          height: "45vh",
                          objectFit: "cover"
                        }}/>

                      <CardBody>
                        <CardTitle className="card-title p-0 text-left">{name}</CardTitle>

                      </CardBody>
                        <Button
                          color="primary"
                          className="w-50 rounded mb-3 mx-auto shadow-sm"
                          style={{
                            borderRadius:"20px"
                          }}
                          onClick={this.handleTaskModal}
                          > View Task
                        </Button>
                    </Card>
                </Col>
            ))}
          </Row>

        </Container>

      </div>
    );
  }

I want to share props between each entry to one modal component 我想在一个模态组件的每个条目之间共享道具

You don't do this in react. 您不会在反应中这样做。 Data always flows from parents to children, never between siblings. 数据总是从父母流向孩子,而不是在兄弟姐妹之间。 Actions can flow up from a child to the parent by providing a handler. 通过提供处理程序,动作可以从孩子流向父母。 The correct way to solve this is to store the currently viewed task in the parent state and display the modal accordingly: 解决此问题的正确方法是将当前查看的任务存储在父状态并相应地显示模式:

class Tasks extends Component {
  state = {
      viewed: null,
  };

  handleShow = name => this.setState({viewed: name});
  handleClose = () => this.setState({viewed: null});    

  render() {
      const {tasks, viewed} = this.state;

      return (
        <div>
          <Container>
            <Row>
              {/* details omitted for simplicity */}
              {tasks.map(({name, image, description}) => (
                <Col key={name}>
                  <Button onClick={() => this.handleShow(name)}>View Task</Button>
                </Col>
              ))}
            </Row>
          </Container>
          {viewed && (
            <TaskModal 
              task={tasks.find(task => task.name === viewed)}
              onClose={this.handleClose}
            />
          )}
        </div>
      );
  }
}

Also note that handlers should modify the state to trigger a re-render. 另请注意,处理程序应修改状态以触发重新渲染。 Returning something from them (eg components) doesn't have any effect. 从它们返回的东西(例如组件)没有任何效果。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM