当孩子也经过时,道具看起来像未定义

我尝试将 a childrenand传递props给我的组件,孩子们传递得很好,但是道具就像未定义的一样,我不明白为什么。当我不使用 children 时,效果很好。一个小片段来理解这个问题。 the function


import React from "react";


export function PropsChildren({ children }, props) {

  console.log("info props", props.name, props.age);

  return (

    <div>

      <p>Props name is {props.name}</p>

      <p>Props age is {props.age}</p>

    </div>

  );

}

the call


import React from "react";

import ReactDOM from "react-dom";

import "./index.css";

//import App from './App';

import reportWebVitals from "./reportWebVitals";

import { PropsChildren } from "./props/props_children";


ReactDOM.render(

  <React.StrictMode>

    <PropsChildren name="Knupel" age={46}>

      {document}

    </PropsChildren>

  </React.StrictMode>,

  document.getElementById("root")

);

控制台返回 info props undefined undefined


GCT1015
浏览 81回答 1
1回答

泛舟湖上清波郎朗

props不是第二个参数。道具和礼物是第一个对象,children也存在于那里。export function PropsChildren({ children, ...props }) {&nbsp; console.log("info props", props.name, props.age);&nbsp; return (&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; <p>Props name is {props.name}</p>&nbsp; &nbsp; &nbsp; <p>Props age is {props.age}</p>&nbsp; &nbsp; </div>&nbsp; );}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript