1

I wrote this code which works

function Circle(props) {
   var style = {
      backgroundColor: "white",
      border: "1px solid black",
      borderRadius: "100%",
      paddingTop: "98%"
   }
   return (<div style={style}></div>)
}

But I want to write it as a class component so I tried

class Circle extends React.Component {         
   var style = {
      backgroundColor: "white",
      border: "1px solid black",
      borderRadius: "100%",
      paddingTop: "98%"
   };   
   render() { return(
      <div style={style}></div>
   )}
}

The error I get is

SyntaxError: Inline Babel script: Unexpected token, expected ( (3:13)
  1 | 
  2 |       class Circle extends React.Component {         
> 3 |          var style = {

I googled a fair amount and found articles like these

https://medium.freecodecamp.org/where-do-i-belong-a-guide-to-saving-react-component-data-in-state-store-static-and-this-c49b335e2a00

Based on this I tried various keywords like static cost and let but nothing seems to work.

Coming from java world, why can't I have the style as the property of my class?

don't flame me I just started to learn react and I did google before asking.

https://codepen.io/knows_not_much/pen/mXXEov

3
  • you aren't returning anything from render Commented Feb 19, 2018 at 1:40
  • updated my question. Commented Feb 19, 2018 at 1:42
  • var is not valid ins a class definition. Commented Feb 19, 2018 at 1:48

2 Answers 2

3

That is not valid as per ES6 class spec.

You can initialise your style in the constructor instead:

class Circle extends React.Component {       
   constructor(props) {
     super(props);
     this.style = {
       backgroundColor: "white",
       border: "1px solid black",
       borderRadius: "100%",
       paddingTop: "98%"
     };
   }     
   render() { return(
      <div style={this.style}></div>
   )}
}

or of course just outside the class:

const style = {
      backgroundColor: "white",
      border: "1px solid black",
      borderRadius: "100%",
      paddingTop: "98%"
};

class Circle extends React.Component {       
   render() { return(
      <div style={style}></div>
   )}
}

or yet another approach, a getter:

class Circle extends React.Component {

   get style() {
      return {
          backgroundColor: "white",
          border: "1px solid black",
          borderRadius: "100%",
          paddingTop: "98%"
      };
   }

   render() { return(
      <div style={this.style}></div>
   )}
}
Sign up to request clarification or add additional context in comments.

1 Comment

my bad - forgot it was a react component class - updated my answer
0

var is not valid in the class definition space. you can simply leave it off:

class Foo {
   style = "bar";
}

var foo = new Foo();
console.log(foo.style);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.