繁体   English   中英

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

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

我试图将一些信息传递给子组件类。 当我通过函数传递它时它正在工作,但如何在一个子类中处理

父组件

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

子组件

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);

工作正常

我的错误,我没有添加道具

现在

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

原为

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM