小编Nak*_*iya的帖子

如何使用更新的 api url 从同一组件调用 getServerSideProps?

我想传递此函数中的参数,以便在应用某些过滤器时在同一页面上获取更新/过滤的数据?

这对于初始渲染工作正常,但我无法从同一组件获取更新的数据,因为该 getServerSideProps 函数位于我的组件之外。

我的组件

let API_URL = `https://api.spaceXdata.com/v3/launches?limit=100`
const spacex = ({ missions }) => {
  const [allMissions, setallMissions] = useState(missions)

  const filterResults = (filter, value) => {
    getServerSideProps(`${API_URL}&${filter}=${value}`) // but this is not accessible from here, getServerSideProps is undefined here as this is outside the function
  }

render() {
 return (
  {missions.map((mission) => <li>mission.name</li>)}
 )
}
Run Code Online (Sandbox Code Playgroud)
export const getServerSideProps = async (API_URL) => {
  const res = await fetch(API_URL)
  const missions = await res.json()
  if (!missions) {
    return …
Run Code Online (Sandbox Code Playgroud)

reactjs server-side-rendering next.js

5
推荐指数
1
解决办法
1146
查看次数

标签 统计

next.js ×1

reactjs ×1

server-side-rendering ×1