bundle.js:sourcemap:30 Uncaught ReferenceError: Cannot access 'Product' before initialization

Created at 10-Nov-2022 , By samar

bundle.js:sourcemap:30 Uncaught ReferenceError: Cannot access 'Product' before initialization. It means you are trying to call the function before initialization. You have to first initialize the function after that you can call it, otherwise it will give you an error message.

The "Cannot access 'Product' before initialization" error occurs, when a function declared using const is accessed before it was initialized in the scope. To solve the error, make sure to initialize the function before accessing it.

Here we first declare the function using const and access the function after the initialization.

  • Example code to initialize product function and call proptypes on it in react.

    You have to install prop-types using npm install prop-types command after that you have to import proptypes using import PropTypes from 'prop-types'.

    import React from 'react';
    import PropTypes from 'prop-types';
    
    const Product = (props) => {
        return (
            <>
                Product Name : {props.name},
                Product Price : {props.price}
    		</>
        )
    }
    
    Product.propTypes = {
        name: PropTypes.string,
        price: PropTypes.number,
    }
    
    export default Product;
    

Back to code snippet queries related react

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.