简体   繁体   English

Onclick 警报并显示反应 js 中的下一项

[英]Onclick the alert and show Next Item In react js

I want to show the Next Item Name when i click on BUSINESS NEWS 1/17/2022 20:00 the alert popup and next item name in display o.r EVENING NEWS 1/17/2022 19:00当我单击 BUSINESS NEWS 1/17/2022 20:00 时,我想显示下一个项目名称,警报弹出窗口和显示中的下一个项目名称 o.r EVENING NEWS 1/17/2022 19:00

Code:-代码:-

         const [comments, setComments] = useState([]);
    const [getname, linkName] = useState([]);

    useEffect(() => {
        callData();
    }, [])

    function callData() {
        axios.get(`http://localhost:9763/api/getRundown`,
            {
                headers: {
                    "accepts": "application/json",
                    'Access-Control-Allow-Origin': '*',
                },
                auth: {
                    username: 'admin',
                    password: 'password'
                },
                parms: {
                    Playlist: 'all'
                }
            })
            .then(response => {
                //setComments([...Object.values(response.data).flat()]);
                //console.log(response.data.Rundowns);
                setComments(response.data.Rundowns);
                //setComments([response.data]);
                //  console.log([response.data]);
            }).catch(error => {
                error('unable to fetch URL', error);
            });
    }

    function myClick(name) {
         alert(`click! ${name} `);
        linkName(name);
    }


    return (
        <div>
            <div className="box">
                <div className="header">
                    <div className="header-text1">Playlists</div>
                    <div className="header-text2"><FaCircle style={{ color: "#10C300", width: "10px", height: "10px" }} /> <FaCircle style={{ color: "#FC0000", width: "10px", height: "10px" }} /></div>
                </div>
                <div>


                    {comments.map((val, index) => {
                        // console.log("Inside map:", getname);
                        return (
                            <div className="TextLink" key={index}>
                                {/* <button onClick={myClick}>{val}</button> */}
                                <NavLink onClick={() => myClick(val)} to={{
                                    pathname: "/tableCheck"
                                }}>
                                    {val}</NavLink>
                            </div>
                        )
                    })
                    }
                </div>
                {/* style={({ isActive }) =>
                                    isActive
                                        ? {
                                            color: 'red',
                                            background: '#151920',
                                        }
                                        : { color: 'white' }
                                }
                                    > */}

            </div>
            <div className="Bottombox">
                <div className="header">
                    <div className="header-text1">Alerts</div>
                    <div className="header-text2"><FaCircle style={{ color: "#10C300", width: "10px", height: "10px" }} /> <FaCircle style={{ color: "#FC0000", width: "10px", height: "10px" }} /></div>
                </div>
                <div style={{ color: "white" }}></div>
            </div>
            <TableCheck val={getname} />
        </div>

    )
}

this upper code is only available to display the current click, not the next item so how can i display the next item name when i click if there is no item for display it shows null in alert此上部代码仅可用于显示当前点击,而不是下一个项目,所以如果没有要显示的项目,当我单击时如何显示下一个项目名称它在警报中显示 null

在此处输入图像描述

you can try to pass the index of the current iteration to the onClick function, and then, access the next value in the array (and make a check to ensure that is not null or empty string).您可以尝试将当前迭代的索引传递给 onClick function,然后访问数组中的下一个值(并检查以确保它不是 Z37A6259CC0C1DAE299A7866489DFFBD0)。 like so:像这样:

<NavLink onClick={(index) => myClick(index)} to={{.....

and try to modify myClick function:并尝试修改 myClick function:

    function myClick(index) {
       if(comments[i+1] !== '' && comments[i+1] !== null) {
         alert(`click! ${name}`);
         linkName(comments[i+1]);
       }
    }
Hope you understood my answer let me know if you got a question or the issue is not solved yet.

From what I can understand (I'm not very clear), you should use useState react hook to cause a re-render so that react knows what item to render next.据我所知(我不是很清楚),您应该使用useState react hook 来重新渲染,以便 react 知道接下来要渲染什么项目。

Can you show the entire code so that I can understand what's going on?您能否显示整个代码,以便我了解发生了什么?

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

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