简体   繁体   English

将值从一个选项卡传递到ReactJs中的另一个选项卡

[英]Pass value from one tab to another tab in ReactJs

I am working on ReactJS and was wondering on how can I pass value between tabs. 我正在研究ReactJS,并想知道如何在选项卡之间传递值。

Below is the component I am working on. 以下是我正在处理的组件。 I have three tab and want to pass the card from one tab to another on clicking accept button. 我有三个标签,并希望通过单击接受按钮将卡从一个标签传递到另一个标签。

import React, { Component } from 'react'
import DriverPlacedOrder from './DriverPlacedOrder';
import {Link }from 'react-router-dom';
import {Tabs,Tab} from 'react-bootstrap';

export default class Driver extends Component {

  constructor(props, context) {
    super(props, context);
    this.state = {
      key: 'home',
    };
  }
  render() {
    return (
      <Tabs
        id="controlled-tab-example"
        activeKey={this.state.key}
        onSelect={key => this.setState({ key })}
      >
        <Tab eventKey="newOrder" title="New Order">

         <div class="container">
                <div class="card" style={{width:'100%',borderRadius:'2%', border: '4px solid lightgreen'}}>
                    <div class="card-body" style={{textAlign:'center'}}>
                        <h4 class="card-title">{CID}</h4>
                        <p class="card-text"><h5>{RID}</h5></p>
                        <Button variant="outline-success"onClick={} style={{width:'33%'}}><i class="fas fa-check-circle fa-lg"></i><br/>Accept</Button>
                        <Button variant="outline-primary" style={{width:'33%'}}><i class="fas fa-book-open fa-lg" fa-lg></i><br/>View</Button>

                    </div>
                </div>
            </div>

        </Tab>
        <Tab eventKey="currentOrder" title="Current Order">

        </Tab>
        <Tab eventKey="orderHistory" title="OrderHistory">

        </Tab>
      </Tabs>
    );
  }
}

Onclicking accept button how can I pass card from NewOrder tab to currentOrder tab? 单击“接受”按钮如何将卡从NewOrder选项卡传递到currentOrder选项卡?

I am working on ReactJS and was wondering on how can I pass value between tabs. 我正在研究ReactJS,并想知道如何在选项卡之间传递值。

<Tab my_data={my_data_value} eventKey="orderHistory" title="OrderHistory">
</Tab>

If I understand your question correctly, you want to re-use the card markup for each tab, and also allow navigation between tabs via that same card markup. 如果我正确理解您的问题,您希望为每个选项卡重复使用卡片标记,并允许通过相同的卡片标记在选项卡之间进行导航。

One way to achieve that would be to define a method such as renderCard() that renders that common markup. 实现这一目标的一种方法是定义一个渲染公共标记的renderCard()方法。 You'd then call that method when rendering the contents of each <Tab> component. 然后在渲染每个<Tab>组件的内容时调用该方法。 Something to keep in mind also is that renderCard() may need a parameter to specify which tab the "Accept" button navigates to: 需要记住的是, renderCard()可能需要一个参数来指定“Accept”按钮导航到哪个选项卡:

export default class Driver extends Component {

    constructor(props, context) {
        super(props, context);
        this.state = { key: 'newOrder' };
    }

    /* Define render card function. Takes nextKey parameter that controls what tab the Accept button will navigate to */
    renderCard(nextKey) {
        return (<div class="card" style={{width:'100%',borderRadius:'2%', border: '4px solid lightgreen'}}>
            <div class="card-body" style={{textAlign:'center'}}>
                <h4 class="card-title">{'CID'}</h4>
                <p class="card-text"><h5>{'RID'}</h5></p>
                <Button variant="outline-success"onClick={ () => {
                this.setState({ key : nextKey })
                }} style={{width:'33%'}}><i class="fas fa-check-circle fa-lg"></i><br/>Accept</Button>
                <Button variant="outline-primary" style={{width:'33%'}}><i class="fas fa-book-open fa-lg" fa-lg></i><br/>View</Button>
            </div>
        </div>)
    }

    render() {
        return (
            <Tabs
            id="controlled-tab-example"
            activeKey={this.state.key}
            onSelect={key => this.setState({ key })}>
            <Tab eventKey="newOrder" title="New Order">
                <div class="container">
                { /* Render card, and specify which tab the card's accept button will navigate to */ }
                { this.renderCard('currentOrder') }
                </div>
            </Tab>
            <Tab eventKey="currentOrder" title="Current Order">
                <div class="container">
                { /* Render card if this tab visible */ }
                { this.renderCard('orderHistory') }
                </div>
            </Tab>
            <Tab eventKey="orderHistory" title="OrderHistory">
                <div class="container">
                { /* Render card if this tab visible */ }
                { this.renderCard('newOrder') }
                </div>
            </Tab>
            </Tabs>
        );
    }
}

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

相关问题 如何在本机反应中将值从一个选项卡发送到另一个选项卡 - How to send value from one tab to another tab in react native 如何在Headless Chrome中将会话Cookie从一个选项卡传递到另一个选项卡 - How to pass session cookies from one tab to another in Headless Chrome 是否有任何方法可以使用 Javascript 从一个浏览器选项卡另一个浏览器选项卡传递实时数据 - Is there any method to pass live data from one Browser tab another Browser tab by using Javascript 将值从一个组件传递到数组 ReactJS 中的另一个值 - Pass value from one component to another values in array ReactJS 如何在选项卡视图页面中从一个选项卡切换到另一个选项卡? - How to shift from one tab to another in a page of tab view? 在dojo中将数据从一个选项卡绑定到另一个选项卡(在选项卡打开?) - Binding data from one tab to another in dojo (on tab open?) 如何将用户从一个选项卡重定向到另一页的另一选项卡? - How to redirect an user from one tab to another tab of other page? 如何将文本从一个标签复制到另一个标签? - How to copy text from one tab to another? CSS从一个选项卡应用于另一个 - css apply from one tab to another 如何将Tab键从一个元素聚焦到另一个元素? - How to focus of tab from one element to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM