Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 发送参数以获取getInitialProps react和nextjs_Reactjs_Next.js_Server Side Rendering - Fatal编程技术网

Reactjs 发送参数以获取getInitialProps react和nextjs

Reactjs 发送参数以获取getInitialProps react和nextjs,reactjs,next.js,server-side-rendering,Reactjs,Next.js,Server Side Rendering,我正在尝试向getInitialProp函数发送一个参数,以将fecth转换为正确的json 这是我的密码: hepler.js-->在这里,我自己进行了提取 export async function getEvents() { const res = await fetch("https://url/eventos.json"); let new_data = await res.json(); return { events: new_data.data }; } expo

我正在尝试向getInitialProp函数发送一个参数,以将fecth转换为正确的json

这是我的密码: hepler.js-->在这里,我自己进行了提取

export async function getEvents() {
  const res = await fetch("https://url/eventos.json");
  let new_data = await res.json();

  return { events: new_data.data };
}

export async function getDetails(slug) {
  const res = await fetch(`https://myurl/${slug}.json`);
  let data_detail_event = await res.json();
  return { data_detail_event };
}
_app.js//这里我有getInitialProps,效果很好

import App from "next/app";
import ContextProvider from "../provider/ContextProvider";
import fetch from "isomorphic-unfetch";
import {getEvents, getDetails} from '../helper/index'

export default class MyApp extends App {
  static async getInitialProps() {

    const events = await getEvents();
    return {
      events : events.events
    };
  }

  render() {
    const { Component, pageProps } = this.props;
    return (
      <div>
        <ContextProvider events={this.props.events} >
          <Component {...pageProps} />
        </ContextProvider>
      </div>
    );
  }
}
从“下一个/App”导入应用程序;
从“./provider/ContextProvider”导入ContextProvider;
从“同构取消蚀刻”导入提取;
从“../helper/index”导入{getEvents,getDetails}
导出默认类MyApp扩展应用程序{
静态异步getInitialProps(){
const events=等待getEvents();
返回{
事件:events.events
};
}
render(){
const{Component,pageProps}=this.props;
返回(
);
}
}
pages/[id].js

import { useRouter } from "next/router";
import Context from "../../config/Context";
/* Components */
import WordCounter from "../../components/word-counter/WordCounter";

function Post(props) {
  const router = useRouter();
  const context = React.useContext(Context);
  return (
    <React.Fragment>
     <WordCounter />
    </React.Fragment>
  );
}
Post.getInitialProps = async ({ query}) => {
  const detail = await getDetail(query.id) --> here I send the param and it seems never arrive to helper.js, why?
  return {detail}
}
export default  Post
从“下一个/路由器”导入{useRouter};
从“../../config/Context”导入上下文;
/*组成部分*/
从“../../components/word counter/WordCounter”导入WordCounter;
功能岗(道具){
const router=useRouter();
const context=React.useContext(context);
返回(
);
}
Post.getInitialProps=异步({query})=>{
const detail=await getDetail(query.id)-->在这里我发送了参数,但它似乎从未到达helper.js,为什么?
返回{detail}
}
导出默认帖子
问题在哪里?救命啊!
桑克斯

我认为getInitialProps在服务器中运行,而您的助手函数并没有加载到服务器中


在getInitialProps内部使用fetch。

如果在
getDetails
中执行
console.log(slug)
操作,您会得到什么。。我什么都没有得到…如果在Post中创建一个函数,我可以让它工作,比如:`const data=async()=>{const detail=wait getDetails(router.query.id)console.log(detail)return{detail}}data();`但是我不认为这是最好的办法。。