javascript - 当孩子也经过时,道具看起来像未定义
问题描述
我尝试将一个children
and传递props
给我的组件,孩子们通过得很好,但是道具就像未定义一样,我不知道为什么。当我不使用孩子时,这很好。一个小片段来理解这个问题。
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
解决方案
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>
);
}
推荐阅读
- python - 当我单击提交按钮时,我的数据库中没有任何显示
- environment - Xcode 12:面板中的环境覆盖设置未反映在模拟器上
- java - 如何让我的用户界面自我适应 JPanel 的显示和隐藏?
- python - 为什么python web应用程序的前端会抛出异常?
- python - Tweepy:你如何检查一条推文是否被收藏?
- scala - 如何使用 sbt-native-packager 设置 Docker Registry
- node.js - 不捕获快速启动异常
- python-3.x - 使用 numpy 使用 python 进行数据分析。使用 Matplotlib 绘制图形
- python - 如何删除答案(正文中的代码)?
- swift - 为什么让 cimg = CIImage(image: self.img) 后图像旋转 90 度