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

泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
props不是第二个参数。道具和礼物是第一个对象,children也存在于那里。
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>
);
}
添加回答
举报
0/150
提交
取消