React Component Render issue

Can Someone Help me with this React.js component.

Here I want to keep on going inside the local directory choosing the needed directory. I can have multiple Input Tables, thus I have list in every state variable and input_num is the index of the particular state function from the list so as to update the same..

I have got two ways.

Way 1- Using another state variable inside the component


    import { useState, useEffect } from 'react';
    
    export default function InputTable({
        InputTableSelectedPath,
        setInputTableSelectedPath,
        InputTableListFileNames,
        setInputTableListFileNames,
        InputTableFileName,
        setInputTableFileName,
        input_num
    }) {
        const [SelectedPath, setSelectedPath] = useState(InputTableSelectedPath);
        const [ListFileNames, setListFileNames] = useState(InputTableListFileNames);
        const [FileName, setFileName] = useState(InputTableFileName);
    
        useEffect(() => {
            setSelectedPath(InputTableSelectedPath);
            setListFileNames(InputTableListFileNames);
            setFileName(InputTableFileName);
        }, [InputTableSelectedPath, InputTableListFileNames, InputTableFileName]);
    
        // Input Table
        function handleInputTable(e) {
            const selectedValue = e.target.value;
    
            if (selectedValue.endsWith('.csv')) {
                const updatedFileName = [...FileName];
                updatedFileName[input_num] = selectedValue.substring(0, selectedValue.length - 4);
                setFileName(updatedFileName);
                setInputTableFileName(updatedFileName);
                return; // Exit the function if a CSV file is selected
            }
    
            let updatedPath;
    
            if (selectedValue === 'back') {
                updatedPath = SelectedPath[input_num].split("/").slice(0, -1).join("/");
                const updatedFileName = [...FileName];
                updatedFileName[input_num] = '';
                setFileName(updatedFileName);
                setInputTableFileName(updatedFileName);
            } else if(selectedValue === ''){
                updatedPath = ${SelectedPath[input_num]}${selectedValue}
                const updatedFileName = [...FileName];
                updatedFileName[input_num] = '';
                setFileName(updatedFileName);
                setInputTableFileName(updatedFileName);
            } else {
                updatedPath = ${SelectedPath[input_num]}/${selectedValue};
            }
    
            if (updatedPath === SelectedPath[input_num]) {
                return;
            }
    
            const updatedSelectedPath = [...SelectedPath];
            updatedSelectedPath[input_num] = updatedPath;
            setSelectedPath(updatedSelectedPath);
            console.log(updatedPath=${updatedPath});
            console.log(updatedSelectedPath[input_num]);
    
            fetch('http://localhost:3000/directories?path=' + updatedPath)
                .then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok');
                    }
                    return response.json();
                })
                .then(data => {
                    console.log(data.directories);
                    const updatedListFileNames = [...ListFileNames];
                    if (data.csvFiles && data.csvFiles.length > 0) {
                        updatedListFileNames[input_num] = data.csvFiles;
                    } else {
                        updatedListFileNames[input_num] = data.directories;
                    }
                    setListFileNames(updatedListFileNames);
                    setInputTableListFileNames(updatedListFileNames);
                    setSelectedPath(updatedSelectedPath);
                    setInputTableSelectedPath(updatedSelectedPath); 
                })
                .catch(error => {
                    console.error('Error fetching data:', error);
                    // Optionally, reset the InputTableSelectedPath to the previous value on error
                    setInputTableSelectedPath(SelectedPath);
                });
        }
    
        function handleInputTable2(e) {
            const updatedFileName = [...FileName];
            updatedFileName[input_num] = e.target.value;
            setFileName(updatedFileName);
            setInputTableFileName(updatedFileName);
        }
    
        return (
                {ListFileNames[input_num].map((name, index) => (
                    {SelectedPath[input_num].split('/').slice(-4).join("/")}{"/"}{name}
                ))}
        );
    }

This is doing as perfectly as thought of. But then I thought of directly rendering the state variables using setter functions from parent component.

Way 2 –


    import { useState, useEffect } from 'react';

    export default function InputTable({
        InputTableSelectedPath,
        setInputTableSelectedPath,
        InputTableListFileNames,
        setInputTableListFileNames,
        InputTableFileName,
        setInputTableFileName,
        input_num
    }) {
        function handleInputTable(e) {
            const selectedValue = e.target.value;
    
            if (selectedValue.endsWith('.csv')) {
                const tempFileName = [...InputTableFileName];
                tempFileName[input_num] = selectedValue.substring(0, selectedValue.length - 4);
                setInputTableFileName(tempFileName);
                return; // Exit the function if a CSV file is selected
            }
    
            let updatedPath;
    
            if (selectedValue === 'back') {
                updatedPath = InputTableSelectedPath[input_num].split("/").slice(0, -1).join("/");
                const tempFileName = [...InputTableFileName];
                tempFileName[input_num] = '';
                setInputTableFileName(tempFileName);
            } else {
                if (selectedValue === '') {
                    updatedPath = `${InputTableSelectedPath[input_num]}${selectedValue}`;
                    const tempFileName = [...InputTableFileName];
                    tempFileName[input_num] = '';
                    setInputTableFileName(tempFileName);
                } else {
                    updatedPath = `${InputTableSelectedPath[input_num]}/${selectedValue}`;
                }
            }
    
            if (updatedPath === InputTableSelectedPath[input_num]) {
                return;
            }
    
            fetch('http://localhost:3000/directories?path=' + updatedPath)
                .then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok');
                    }
                    return response.json();
                })
                .then(data => {
                    setInputTableListFileNames(prevListFileNames => {
                        const tempListFileNames = [...prevListFileNames];
                        if (data.csvFiles && data.csvFiles.length > 0) {
                            tempListFileNames[input_num] = data.csvFiles;
                        } else {
                            tempListFileNames[input_num] = data.directories;
                        }
                        return tempListFileNames;
                    });
    
                    setInputTableSelectedPath(prevSelectedPath => {
                        const tempSelectedPath = [...prevSelectedPath];
                        tempSelectedPath[input_num] = updatedPath;
                        return tempSelectedPath;
                    });
                })
                .catch(error => {
                    console.error('Error fetching data:', error);
                    setInputTableSelectedPath(InputTableSelectedPath);
                });
        }
    
        function handleInputTable2(e) {
            const tempFileName = [...InputTableFileName];
            tempFileName[input_num] = e.target.value;
            setInputTableFileName(tempFileName);
        }
    
        return (
                {InputTableListFileNames[input_num].map((name, index) => (
                        {InputTableSelectedPath[input_num].split('/').slice(-4).join("/")}/{name}
                ))}
        );
    }

Here, the dropdown with initial states come as perfectly. And when chosen the first path, the directories inside are also visible on the console. But this function


    setInputTableListFileNames(prevListFileNames => {
        const tempListFileNames = [...prevListFileNames]; // Create a new array based on previous state
        if (data.csvFiles && data.csvFiles.length > 0) {
            tempListFileNames[input_num] = data.csvFiles;
        } else {
            tempListFileNames[input_num] = data.directories;
        }
        return tempListFileNames; // Return the updated state
    });

It is not updating the InputTableListFileNames. I am unable to find the reason. Can you please help??

New contributor

Ankit Karn 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