简体   繁体   English

我试图在 nextjs 中将数据从一个页面发送到另一个页面但失败了

[英]I am trying to send data from one page to another in nextjs but failing

Its giving this error "Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead."它给出了这个错误“错误:对象作为 React 子项无效(找到:object,键为 {})。如果您打算呈现子项集合,请改用数组。” Please help.请帮忙。

export default function PostScreen() {
      const data = 'Hello';
      return (
        <>
          <div>
            <span>Create Post</span>
    
            <Link
              href={{
                pathname: '/details',
                query: data, 
              }}
            >
              <a>Click me</a>
            </Link>
          </div>
        </>
      );
    }
    
    // Another page
    function Details(){
      const router = useRouter();
      const data = router.query;
      return (
        <div>
          <div>
            <div>
              <strong>Username:</strong> {data}
            </div>
          </div>
        </div>
      );
    }

query in href accepts an object with key value pairs. href中的query接受带有键值对的 object。 If you pass string , it will convert the variable's value of the string to key and its value will be empty;如果传递string ,它会将字符串的变量值转换为key ,其值为空;

If you do a console.log(data) on the /details page, you get the following:如果你在/details页面上执行console.log(data) ,你会得到以下信息:

{ Hello: '' }

React doesn't support rendering JS objects in the JSX. React不支持在 JSX 中渲染 JS 对象。 Hence, you see that error.因此,您会看到该错误。

So, to solve your problem, you can give some key to the data and use that key in /details page to get your data.因此,要解决您的问题,您可以为数据提供一些密钥,并在/details页面中使用该密钥来获取您的数据。

For example:例如:

export default function PostScreen() {
  const data = 'Hello';
  return (
    <div>
      <span>Create Post</span>
      <Link
        href={{
          pathname: '/details',
          query: { message: data }, 
        }}
      >
        <a>Click me</a>
      </Link>
    </div>
  );
}
    
// "/details" page
function Details(){
  const router = useRouter();
  const data = (router.query ?? {}).message;
  return (
    <div>
      <div>
        <div>
          <strong>Username:</strong> {data}
        </div>
      </div>
    </div>
  );
}

Route.query is a object. you should unpack it first, like: Route.query 是一个 object。你应该先解压它,比如:

const {id} = data

Then pass id to the HTML然后将id传给HTML

暂无
暂无

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

相关问题 NextJS:如何将数据从一页发送到另一页的getInitialProps函数 - NextJS: How to send data from one page to another page's getInitialProps function 我试图将一些表单数据从一个html页面传递到另一个页面 - I am trying to pass some form data from one html page to another 如何将数据发送到另一个 NextJS 页面? - How to send data to another NextJS page? 将数据从一个表发送到另一页 - send data from one table to another page 离线Google Chrome扩展程序/打包应用程序:尝试将表单数据从一个本地页面发送到另一个本地页面 - Offline Google Chrome extension/packaged app: Trying to send form data from one local page to another 我正在尝试使用 ajax POST 方法从 html 页面将数据发送到我的 PHP 页面,但它给出错误,注意:未定义索引: - I am trying to send data to my PHP page from html page using ajax POST method but it give error ,Notice: Undefined index: 我正在尝试使用Ajax将数据发送到另一个页面 - I'm trying to use Ajax to send data to another page Html 如何将数据从一页发送到另一页 - Html how send data from one page to another page 尝试通过ipc将数据从一个电子窗口发送到另一个电子窗口 - Trying to send data from one electron window to another via ipc 当我尝试从一个页面导航到另一页面时,undefined不是对象(评估&#39;_this.props.navigator.push&#39;)是本机的 - undefined is not an object (evaluating '_this.props.navigator.push') react native , when i am trying to navigate from one page to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM