简体   繁体   English

ReactJS:子组件中未定义道具,我无法通过父组件传递道具参数

[英]ReactJS: Props is undefined in child component and I can't pass props parameters throught parent

I'm learning React and I have a problem trying to read my props in a child component.我正在学习 React,但在尝试读取子组件中的道具时遇到问题。

The child component is:子组件是:

const HelloWorldApp = ( props ) => {
  console.log(props);
  return (<>
    <h2> {props} </h2>
    </>
  );
}
export default HelloWorldApp;

The parent is:父母是:

import React from 'react';
import {createRoot} from 'react-dom/client';
import HelloWorldApp from './FirstApp';
import { SecondApp } from './SecondApp';
import './styles.css';


createRoot(document.getElementById('root')).render(HelloWorldApp());

createRoot(document.getElementById('raiz')).render(SecondApp());

1) What is the problem? 1)有什么问题? 2) How can I pass props from the parent to the children component? 2)如何将道具从父组件传递给子组件?

I think the problem is in the render function.我认为问题出在渲染 function 中。 You need something like this:你需要这样的东西:

createRoot(document.getElementById('root'))
     .render(<HelloWorldApp />);
  1. In react, you don't need to render all the components in separate CreateRoot..在反应中,您不需要在单独的 CreateRoot.. 中渲染所有组件。

  2. render your child component with props inside the parent component使用父组件内的道具渲染您的子组件

The parent Component父组件


import React from 'react';
import {createRoot} from 'react-dom/client';
import HelloWorldApp from './FirstApp';
import './styles.css';


createRoot(document.getElementById('root')).render(<HelloWorldApp data="propsData" />);


The child Component子组件


const HelloWorldApp = ( props ) => {
  console.log(props);
  // {data: "dummyData"}
  return (<>
    // you can't able to render object inside the jsx
    <h2> {props.data} </h2>
    </>
  );
}
export default HelloWorldApp;

The structure React program is on constantly changing over the time, whereby i recommend you use command npm create-react-app to have the latest standards of programming in that framework. React 程序的结构会随着时间不断变化,因此我建议您使用命令npm create-react-app在该框架中拥有最新的编程标准。 Only one createRoot is necessary in React renderize DOM, and i recommend you receive parameters with destructuring in child component, the parameters must been passed in JSX tags as attributes在 React 渲染 DOM 中只需要一个createRoot ,我建议你在子组件中接收带有解构的参数,这些参数必须作为属性在 JSX 标签中传递

暂无
暂无

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

相关问题 我可以在 Astro 中将道具从子组件传递给父组件吗? - Can i pass props from a child component to parent in Astro? 当我通过 props 从父组件将变量的值传递给子组件时,我收到一个未定义的值 - I receive an undefined value when I pass the value of a variable through props from a parent component to a child component 子组件将 props 传递给父组件的 useState -&gt; 返回未定义(反应) - child component pass props to parent's useState -> returning undefined (react) 如何通过在 ReactJS 中将 props 从父组件传递到子组件来初始化状态? - How can i initialize state by passing props from parent component to child component in ReactJS? 当我将 function 传递给子组件时,ReactJS this.props.* 不是 function - ReactJS this.props.* is not a function when I pass a function to child component 我们可以在 React 中将 props 从 Parent 传递给 Child 组件,并将 props 设置为子组件的 state 吗? - Can we pass props from Parent to Child component in React, and set the props as state of child component? ReactJS:从父组件道具传递要映射的道具是未定义的 - ReactJS: Passing props to map from parent component props are undefined 如何将道具从子组件传递到父组件到 ReactJS 中的另一个子组件? - How to pass props from child component to parent component to another child component in ReactJS? 如何通过道具[REACT.JS]将图片网址从父组件传递到子组件 - How can I pass image url from parent component to child component via props [REACT.JS] 将道具传递给 ReactJS 中的组件 - Pass props to component in ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM