-1

I got the unexpected token error in innerFunc, I have no clue why is it so.

const TestComp = () => {

  innerFunc(){ //error here?
    console.log('xx')
  }

  render() {
    return(
      <div>test comp content</div>
    )
  }
}

const Something = ({
  something
}) => {
  return(
    <div>{TestComp.toString()}</div>
  )
}

Any clue why?

1
  • Could you provide some information on the type of error, stacktrace of the error etc to make it easier to debug your example please? Commented Jan 12, 2018 at 9:41

2 Answers 2

1

This right here:

const TestComp = () => { // <---- Function starts

  innerFunc(){ // function inside a function needs to be declared with `function` identifer
    console.log('xx')
  }

  render() {
    return(
      <div>test comp content</div>
    )
  }
} // <---- Function ends

is a function.

You are trying to add attributes to this function, which is causing the error.

You can add attributes to the class like the way you done it.

To solve the error define a function instead like such:

const TestComp = () => {

  function innerFunc(){
    console.log('xx')
  }

  return( // you don't need to define render here, just return the jsx
    <div>test comp content</div>
  )
}

Or better yet, define it outside this function based component.

I hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Wouldn't render() also require function?
Oh right! Forgot about that. Actually you dont need render at all, just return the jsx.
how to call innerFunc? in jsx <div onClick={()=>this. innerFunc()} >test</div> I got innerFunc of undefined
just to onClick={innerFunc} without this.
0

You have mixed up the syntaxes of a functional stateless component and a react component

class TestComp extends React.Component {

  innerFunc(){ //error here?
    console.log('xx')
  }

  render() {
    return(
      <div>test comp content</div>
    )
  }
}

2 Comments

is this equivalent to React.createClass?
React.createClass is deprecated from v16 onwards, its ES6 style of writing a React component, check this stackoverflow.com/questions/46482433/…

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.