简体   繁体   English

单击按钮时有条件地呈现组件(React)

[英]Conditionally rendering component on button click (React)

I have created a basic React project that is pulling data from a SQL server.我创建了一个从 SQL 服务器中提取数据的基本 React 项目。 I would like this to be able to be rendered conditionally depending on what button has been clicked.我希望能够根据单击的按钮有条件地呈现它。

This is my Display Users Component which is used within my AdminViewUsers component (What is actually displaying the users).这是我的显示用户组件,它在我的 AdminViewUsers 组件中使用(实际显示用户的是什么)。

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

class DisplayUsers extends React.Component{
  constructor(){
    super();

    this.state= { users: [] } 
  }
  componentDidMount(){
   this.setState({
      users: this.getItems()
   })
  }

  getItems(){
fetch('/admin-view-users')
.then(recordset => recordset.json())
.then(results => { console.log(results.recordset); this.setState({'users': results.recordset}); });

  }

  render () {

    console.log(this.state.users)
    return (
    <ul>
      {this.state.users && this.state.users.map(function(user, index){

        //if (user.severity === 1){          
        return(
          <div className ="jumbotron">
        <li>
          Severity: {user.severity}
        </li>
        <li>
          <p>User Name:{user.name} </p>
        </li>

        <li>
        User Email: {user.email}
        </li>
        <li>
          Description of Issue: {user.description} 
          </li>
          <button>See Details</button>
          </div>
        )
        })
         } 
     </ul>   
    );    
  }
}

export default DisplayUsers; 

This is my AdminViewUsers Component这是我的 AdminViewUsers 组件

import logo from '../codestone logo.png';
import {Link } from 'react-router-dom'
import '../bootstrap.min.css'

import '../bootstrap.min.css'
import '../App.css'

import  Customers  from "../Components/customers";
import  DisplayUsers  from "../Components/DisplayUsers";

import { ButtonDropdown, DropdownToggle, DropdownMenu, DropdownItem, DropDownButton } from 'reactstrap';

function Home() {
  return (

      <div> 
        <Header/>
        <SeveritySelector/>
        <DisplayUsers/>
     </div>
  );
}

function Header(){
    return (
      <div class="jumbotron">
        <div className = "User-Menu">
      <Link>User details  </Link>
      </div>
      <img className='profile-image' alt='icon' src={logo} width="340" height="60"/>
     <Navigation/>
    </div>
      )
    }

    function Navigation (){
      return(
        <div>
          <br/>
          <div class="btn-group">
     <Link to= '/home'><button type="button" class="btn btn-light">Home</button></Link>
     <Link to= '/admin-view-users'><button type="button" class="btn btn-light">View Users(Admin)</button></Link>
    </div>
      </div>
      )
    }

    function SeveritySelector (){
      return(
         <div className = "Severity-Toolbar">
        <div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
        <div class="btn-group mr-2" role="group" aria-label="First group">
        <button type="button" class="btn btn-secondary">Severity High</button>
        <button type="button" class="btn btn-secondary">Severity Medium</button>
       <button type="button" class="btn btn-secondary">Completed</button>
       <button type="button" class="btn btn-secondary">View All</button>
  </div>
        </div>
        </div>
      )
    }

  export default Home;

Essentially I would like to use the function Severity Selector to be the decider of how the statement is displayed.Eg If the high severity button is selected then it will display all with a high severity (1) if medium selected all with medium severity (2) and completed to have a severity of 3. Finally a button to display all.本质上,我想使用 Severity Selector 函数来决定语句的显示方式。例如,如果选择了高严重性按钮,那么它将显示所有具有高严重性 (1) 如果中选择所有具有中等严重性 (2) ) 并完成了严重性为 3。最后一个按钮来显示所有。

What in your opinion is the best way to do this?您认为最好的方法是什么? I understand I could use multiple statements within my "server.js" and load different queries and have them connected to different pages.我知道我可以在“server.js”中使用多个语句并加载不同的查询并将它们连接到不同的页面。

But is there a way that I could just use a if statement or something similar to determine what button is selected to avoid multiple transactions with the server?但是有没有一种方法可以只使用 if 语句或类似的东西来确定选择了哪个按钮来避免与服务器进行多次交易? You can see a brief attempt I had within the display users with an if statement which worked but just was not dependent on the buttons.您可以看到我在显示用户中使用 if 语句进行的简短尝试,该语句有效但不依赖于按钮。

Conditional render can be achieved using various techniques, the most used is the bracket style variant.可以使用各种技术实现条件渲染,最常用的是括号样式变体。 It can be used in the following way:它可以通过以下方式使用:

function Header(){
    const showFirst = true;

    return (
      <div class="jumbotron">
         {showFirst && <MyFirstComponent />}
         {!showFirst && <MySecondComponent />}
       </div>
      )
    }

This will render the <MyFirstComponent /> if showFirst is true and will show <MySecondComponent /> if it is false.这会使得<MyFirstComponent />如果showFirst是真实的,将显示<MySecondComponent />如果是假的。

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

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