[英]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.