I have a react component that is trying to pass a function to another component, the problem is that I’m not being able to define the function, it throws a compiling error
JavaScript
x
16
16
1
export default function App() {
2
3
createActivity() { // here I get an error: missing semicolon
4
console.log("creating activity");
5
}
6
7
return (
8
<div className = "App" >
9
<Route path="/" component={ Header } />
10
<Route exact path="/" component={ShowSplashWindow} />
11
<Route path="/createactivitiy" render = {() =>
12
<CreateActivity createActivity={this.createActivity} />} />
13
</div>
14
);
15
}
16
What am I missing?
Rafael
Advertisement
Answer
You declared component as functional, but trying to create a class method.
You should either use class component:
JavaScript
1
2
1
export default class App extends React.Component {
2
with 2 methods: createActivity
and render
.
Or declare your function and assign to constant
JavaScript
1
2
1
const createActivity = () =>
2
but use it without this
:
JavaScript
1
2
1
<CreateActivity createActivity={createActivity} />
2