Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REACT JSX styles [object object]

Tags:

reactjs

i'm studying about React but don't understand the JSX Styles. here code.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
    <div id="disp"></div>
    <script type="text/babel">
        var root = document.querySelector('#disp')
        const cssStyle = {
            'color': 'red',
            'backgroud-color': '#fff0f0',
            'font-size':'2em'
        }
        const scriptStyle = {
            color: 'blue',
            backgroundColor: '#fff0f0',
            fontSize: '2em'
        }
        function getDOM() {

            return (
                <div> 
                    <div style={scriptStyle}>
                        Look at the stars
                    </div>
                    <p stlye={cssStyle}>
                        Look how they shine for you
                    </p>
                </div>
            )
        }
        ReactDOM.render(getDOM(), root)
    </script>
</body>
</html>

Issue

scriptStyle is good but I can not see cssStyle normally. [object object] so how can I?

like image 718
soo Avatar asked Nov 18 '18 13:11

soo


People also ask

Can I use style in JSX?

JSX gives us the convenience of combining HTML syntax and JavaScript under one file in React. However, our stylesheets always existed in a separate directory. With inline styles, you have to option to combine CSS syntax with JSX code.

Can we use style tag in React?

Did you know that you can style a React component with raw CSS and no extra files? There are many ways to style a component in React, but some are more popular than others.

What is a JSX object?

JSX stands for JavaScript syntax extension. It is a JavaScript extension that allows us to describe React's object tree using a syntax that resembles that of an HTML template. It is just an XML-like extension that allows us to write JavaScript that looks like markup and have it returned from a component.


1 Answers

In JSX, styles are provided as an object, with keys being camelCase instead of snakeCase. For instance background-color will be written as backgroundColor which is exactly how it is being provided in scriptStyle object and that is how it needs to to given to the style. Apart from it you also have a typo in style={cssStyle}

Working demo

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
    <div id="disp"></div>
    <script type="text/babel">
        var root = document.querySelector('#disp')
        const cssStyle = {
            'color': 'red',
            'backgroudColor': '#fff0f0',
            'fontSize':'2em'
        }
        const scriptStyle = {
            color: 'blue',
            backgroundColor: '#fff0f0',
            fontSize: '2em'
        }
        function getDOM() {

            return (
                <div> 
                    <div style={scriptStyle}>
                        Look at the stars
                    </div>
                    <p style={cssStyle}>
                        Look how they shine for you
                    </p>
                </div>
            )
        }
        ReactDOM.render(getDOM(), root)
    </script>
</body>
</html>

Docs Link

like image 147
Shubham Khatri Avatar answered Sep 30 '22 02:09

Shubham Khatri