English 中文(简体)
我如何在下几页使用外部java书写变量?
原标题:How can I use the external javascript variables in a nextjs page?

Now I m building a webpage that shows a ranking of items. I m fetching datas with an external javascript file and using it by putting tag in nextjs page. The script uses the window variable, so I couldn t make it API and execuse it on the serverside.

Now my page is like below.

<div>
<Script src="/weeklyRanking.js" />
</div>

在小节中,我有各种项目,这是一个阵列,我希望在以下一页中使用。

{items.map((item)=>(
 <div>{item}<div>
))}

我怎么能够把文字标签的变数上下页?

I tried below, weeklyRanking.js

let items = [item1,item2,item3];
window.items = items;

页: 1

import Script from  next/script 

const page = () => {
  const [items, setItems] = useState([]);
  }
  useEffect(() => {
    setItems(window.items)
  }, [])
  
  return (
    <>
    {items.map((item)=>(
    <div key={item}>{item}</div>
    ))}
    <Script src="/weeklyRanking.js" />
    </>
  )
}

export default page

The result is the items variable is undefined.

问题回答

为了将外部Java文本的变数传送到您的下页,你可以修改每周排名的文字。 j 将项目变数定为全球变量。 在这方面,你可以做些什么:

In weeklyRanking.js: javascript // Define the items variable as a global variable window.items = [item1, item2, item3];

// Rest of your code

In your Next.js page: jsx

import Script from  next/script ;
import { useEffect, useState } from  react ;

function YourPage() {
  const [items, setItems] = useState([]);

  useEffect(() => {
    // Access the global items variable after the script has loaded
    setItems(window.items);
  }, []);

  return (
    <div>
      {items.map((item) => (
        <div key={item}>{item}</div>
      ))}
      <Script src="/weeklyRanking.js" />
    </div>
  );
}

export default YourPage;




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签