Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected keyword 'this' reactjs jsx [duplicate]

Tags:

reactjs

jsx

I'm new to reactjs. I'm trying to put a condition in the render return method to show component. I'm getting the following error.

./components/Layouts/Header.js
SyntaxError: /home/user/Desktop/pratap/reactjs/society/society-front/components/Layouts/Header.js: Unexpected keyword 'this' (14:8)

  12 |   render() {
  13 |     return (
> 14 |       { this.props.custom ? <CustomStyle /> : <DefaultStyle /> }
     |         ^
  15 |     );
  16 |   }
  17 | }

Here is my component code -

import React from "react";
import CustomStyle from "./CustomStyle";
import DefaultStyle from "./DefaultStyle";

class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      custom:this.props.custom
    }
  }
  render() {
    return (
      { this.props.custom ? <CustomStyle /> : <DefaultStyle /> }
    );
  }
}

export default Header;
like image 569
Mahendra Pratap Avatar asked Jul 30 '19 13:07

Mahendra Pratap


1 Answers

You can't return an operator when you are explicitly returning JSX, Wrap your code in a Fragment:

  render() {
    return (
      <>{ this.props.custom ? <CustomStyle /> : <DefaultStyle /> }</>
    );
  }

Or remove the separator:

render(){
    return this.props.custom ? <CustomStyle /> : <DefaultStyle />
}
like image 174
Dupocas Avatar answered Oct 22 '22 21:10

Dupocas