im trying to pass a string to a function using onClick method, here is my code :
App.js
import React from 'react';
import logo from './logo.svg';
import './App.css';
const todos = [
{id: 1, nama: 'belajar', status: 'selesai'},
{id: 2, nama: 'makan', status: 'belum selesai'}
]
export class MyTodos extends React.Component{
constructor(){
super();
this.sayHello = this.sayHello.bind(this);
}
}
function sayHello(name) {
alert(`hello, ${this.name}`);
}
function App() {
return (
<div className="App">
<div className="Navbar">
<nav className="navbar navbar-dark bg-dark rounded-pill col-sm-8">
<input type="text" className="form-control form-control-sm col-sm-6 rounded-pill" name="aktivitas"></input>
<button className="btn form-control form-control-sm col-sm-4 bg-light rounded-pill" onClick={() => sayHello(this.aktivitas)}>
Tambah
</button>
</nav>
</div>
<br></br><br></br>
<div className="Kartu card">
<ul className="list-group list-group-flush">
{todos.map(todos => <li className="list-group-item">{todos.nama} : {todos.status}</li>)}
</ul>
</div>
</div>
);
}
export default App;
the expected output is, when i type something inside the input field it will give me an alert that says Hello, <inputted string> For example if i type name inside the input field it will gave me hello, name
i guess it have something to do with binding something, i tried to bind it bit it gave me this error :
TypeError: Cannot read property 'aktivitas' of undefined
thanks before, any help will be appreciated