How to use the selected value from a dropdown component as the ‘filter’ value in another component?

I have a component that creates a dropdown where the values are a list of policy areas that are pulled from a SQL server database/tables. The selected policy area is then saved into a useState called selectedPolicyArea. I then have a second component that I want to display the stakeholder who is in charge of the policy area, where whenever the selected policy area is changed the stakeholder name will change with it. For example: if Policy Area 1 is selected I expect the stakeholder name John Doe to be the only thing in the stakeholder box, but then if Policy Area 8 is selected then I would expect the stakeholder should switch to Jane Doe.

Dropdown Component and Current Stakeholder component

What console looks like when value is selected from dropdown and what the data looks like being pulled in from the stakeholder component

Here is the code for my Dropdown Component (I did raiase the state up to the parent component so I can use props as this selected value is used in another component as well)

import React, { useState, useEffect } from 'react';

const DropdownGetPolicyArea = (props) => {
 const [policyAreaName, setPolicyAreaName] = useState([]);
 const [selectedPolicyArea, setSelectedPolicyArea] = useState('');

 useEffect(() => {
    fetch('/GetPolicyAreaName', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      }
    })
      .then(res => res.json())
      .then(data => setPolicyAreaName(data))
      .catch(error => console.error('Error fetching data: ', error));
 }, []);

 const handleChange = (event) => {
    setSelectedPolicyArea(event.target.value);
    props.setSelectedPolicyArea(event.target.value)
 };

console.log(selectedPolicyArea)

 return (
    <div className='DropdownGetPolicyArea'>
      <h2>Policy Area Name</h2>
      <select  value={selectedPolicyArea} onChange={handleChange}>
        <option  value=''>...</option>
        {policyAreaName.map((category) => (
          <option key={category.Policy_Area_ID} value={category.Policy_Area_Name}>
            {category.Policy_Area_Name}
          </option>
        ))}
      </select>
    </div>
 );
};


export default DropdownGetPolicyArea;

Here is the code for my currentPolicyStakeholder component

import React, { useEffect, useState } from 'react';

const CurrentPolicyStakeholder = (props) => {
    const [stakeholder, setStakeholder] = useState([]);

    useEffect(() => {
        const fetchStakeholder = async () => {
            try {
                const response = await fetch('/GetStakeholder');
                const data = await response.json();
                setStakeholder(data);
            } catch (error) {
                console.error('Error fetching stakeholder:', error);
            }
        };
        fetchStakeholder();
    }, []);

    console.log(stakeholder);

        return(
            <div>
                <ul>{stakeholder.Employee_Preferred_Full_Name}{props.selectedPolicyArea}</ul>

            {stakeholder.map((stakeholders) => (
                <option>
                    {stakeholders.Stake_Table_PrimaryKey_ID} 
                    --
                    {stakeholders.Employee_Preferred_Full_Name}
                    --
                    {stakeholders.Employee_ID}
                </option>
            
        ))}
            </div>
    );

}
    
 
export default CurrentPolicyStakeholder;

My 2 APIs are:

app.get('/GetPolicyAreaName', async(req,res) =>{
    console.log('policy area called');
    const policyAreaName = await dbOperations.getPolicyAreaName()
    res.send(policyAreaName.recordset)
    console.log('called');
})


app.get('/GetStakeholder', async(req,res) =>{
    console.log('stakeholder called');
    const stakeholderName = await dbOperations.getCurrentActivePolicyStakeholder()
    res.send(stakeholderName.recordset)
    console.log('called');
})

The 2 queries where the data is being pulled from:

const getPolicyAreaName = async(policyAreaName) => {
    try {
        let pool = await sql.connect(config);
        let policyAreaName = await pool.request().query(`SELECT Policy_Area_ID,Policy_Area_Name from Policy_Area`)
        return policyAreaName;
    }
    catch(error) {
        console.log(error);
    }
}


const getCurrentActivePolicyStakeholder = async() => {
    try {
        let pool = await sql.connect(config);
        let stakeholderName = await pool.request()
        .query(`SELECT S.[Stakeholder_ID]
                ,S.[Employee_ID]
                ,S.[Employee_Preferred_Full_Name]
                ,S.[Stakeholder_Role_ID]
                ,S.[Stake_Table_Name]
                ,S.[Stake_Table_PrimaryKey_ColName]
                ,S.[Stake_Table_PrimaryKey_ID]
                ,PA.Policy_Area_Name
        
  
                FROM [LegoTest].[dbo].[Stakeholder] S
                join [LegoTest].[dbo].[Policy_Area] PA  ON    PA.Policy_Area_ID = S.Stake_Table_PrimaryKey_ID

                WHERE   S.Stake_Table_Name = 'Policy_Area'`
            )
        return stakeholderName;
    }
    catch(error) {
        console.log(error);
    }
}

I attempted to do some condtional rendering with if/else and also ternary operator but could only ever get it to return the entire list of names from the stakeholder table from the database. I had thought that I could potentially use template literals in the getCurrentActivePolicyStakeholder SQL query under the ‘WHERE’ clause but I was unsuccessful at inplementing that.

Any and all help/advice is greatly appreciated!

New contributor

Riley Grogan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật