-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?

Xie Xie Fang
  • 181
  • 3
  • 9
  • Could you provide some information on the type of error, stacktrace of the error etc to make it easier to debug your example please? – bbop99 Jan 12 '18 at 09:41

2 Answers2

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.

Dangling Cruze
  • 3,283
  • 3
  • 32
  • 43
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>
    )
  }
}
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • is this equivalent to React.createClass? – Xie Xie Fang Jan 12 '18 at 09:48
  • React.createClass is deprecated from v16 onwards, its ES6 style of writing a React component, check this https://stackoverflow.com/questions/46482433/reactjs-createclass-is-not-a-function/46482830#46482830 – Shubham Khatri Jan 12 '18 at 09:49