0

I am trying to write a function with properties but keep getting a syntex error.

I need to define my state but not sure where this should be placed. What is the best way to write the below code?

UPDATED--
class TEST extends React.Component {
   constructor(props) {
   super(props);
 }
state = {isApp: false,}
 myFunction  = ({ isApp }) => {
    return (
    <div>
       <p>Hello World</p>
    </div>
  )}
}

export default TEST;

I am guessing that my arrow function should not be within the class?

The syntax error has gone but now the page does not load at all, I guess it doesn't know what to do with "myFunction "

3
  • Maybe post the error you're getting? Commented Jul 23, 2020 at 11:47
  • Syntax error: Unexpected token (24:11) 22 | } 23 | state = {isApp: false,} > 24 | const myFunction = ({ isApp }) => { | ^ 25 | return ( 26 | <div> 27 | {!isApp && Commented Jul 23, 2020 at 11:49
  • Remove const in front of myFunction and you forgot the closing paren on the return. You have one too many closing curly braces Commented Jul 23, 2020 at 11:52

2 Answers 2

1

Initial state should be assigned to the instance in the constructor:

class TEST extends React.Component {
 constructor(props) {
   super(props);
   this.state = {isApp: false};
 }

 myFunction  = ({ isApp }) => {
    return (
    <div>
       <p>Hello World</p>
    </div>
  )};
}
Sign up to request clarification or add additional context in comments.

Comments

0

You probably don't need an arrow function here. The body of that function could be placed inside the render function. As well, feel free to remove the constructor.

class TEST extends React.Component {
  state = {isApp: false,}
  render() {
    const {isApp} = this.state;
    return (
      <div>
          {!isApp  && <p>Hello World</p>}
      </div>
    );
  }
}

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.