简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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