简体   繁体   English

如何在React中将props从父组件(函数)传递到子组件(CLASS)

[英]How to pass props from parent component(function) to child component(CLASS) in React

I am trying to pass some info to a child component-class. 我试图将一些信息传递给子组件类。 It is working when im passing it through functions, but how to handle in a class-child 当我通过函数传递它时它正在工作,但如何在一个子类中处理

Parent component 父组件

export default function Header(props) {
  const propToChild = 'thisisprop'
  return (<FormLayouts propToChild={propToChild}/>)
}

Child component 子组件

import React from 'react';
import { withStyles, makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles(theme => ({
    root: {
        width: '100%',
    },
}));

class FormLayouts extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            open: false,

        };
    }
  render() {
    console.log(this.props);  //it`s empty((
  }
}

export default withStyles(useStyles)(FormLayouts);

Its working fine 工作正常

My mistake, i didnt add prop to state 我的错误,我没有添加道具

now 现在

const [currentWindow, setCurrentWindow] = React.useState(<FormLayouts urls={props.urls}/>);

was 原为

const [currentWindow, setCurrentWindow] = React.useState(<FormLayouts />);

暂无
暂无

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

相关问题 如何在本机反应中将道具从父组件传递到子组件? - How to pass props from parent component to child component in react native? 反应:将 2 个道具从子组件传回同一 function 中的父组件 - React: Pass 2 props back to the parent component in the same function from the child 如何将功能作为道具从功能父组件传递给子组件 - How to pass function as props from functional parent component to child 如何将道具从子组件传递给父组件? - How to pass props from child component to parent? 如何在 React 中将函数从子组件传递给父组件 - How to Pass a function from Child component to Parent Component in React React,如何将道具从父组件发送到子组件? (在渲染 function 之外使用这些道具) - React, How to send props from parent component to child component? (to use those props outside render function ) 如何使用 onClick 事件将 props 从子组件传递到父组件 React Hooks - How to use onClick event to pass the props from child component to parent component React Hooks 如何使用 React Native 中的 props 将数组从父组件传递到子组件? - How to pass an array from a parent component to child component using props in React Native? 如何通过道具[REACT.JS]将图片网址从父组件传递到子组件 - How can I pass image url from parent component to child component via props [REACT.JS] 如何在 React 中将异步道具从父组件传递到子组件 - How to pass async props from parent-component to child-component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM