I have a <Table />
which gets dynamically popoulated with data from an object array. I want to add an onClick
event to the <TableRow />
so the data in that row can be updated. onClick
event does not seem to call my openBookDetails
function.
import React, { Component } from 'react';
import './Update.css';
import Search from '../Search/Search';
const Table = ({ data }) => (
<table class="table table-hover">
<thead>
<tr class="table-primary">
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">ISBN</th>
<th scope="col">No. Of Copies</th>
</tr>
</thead>
<tbody>
{data.map(row =>
<TableRow key={row.id} row={row} />
)}
</tbody>
</table>
)
const TableRow = ({ row }) => (
<tr class="table-light" onClick={this.openBookDetails}>
<th scope="row" >{row.title}</th>
<td >{row.author}</td>
<td >{row.isbn}</td>
<td >24</td>
</tr>
)
class Update extends Component{
constructor(props) {
super(props);
this.state = {
value: '',
suggestions: [],
setOfAllBooks: [],
searchedBooks: []
};
this.openBookDetails = this.openBookDetails.bind(this);
this.setTableData = this.setTableData.bind(this);
}
setTableData(searchedBook){
this.setState({searchedBooks: searchedBook})
console.log(this.state.searchedBooks)
}
openBookDetails(){
console.log("openBookDetails")
}
render(){
return(
<div>
<Search state={this.state} setTableData={this.setTableData} />
<Table data={this.state.searchedBooks} />
</div>
)
}
}
export default Update;
Your TableRow component is a stateless (presentional) component so you cannot use this
inside of it. The openBookDetails function is in the class-component Update so it's not in TableRow but in Table's parent : Update.
You need to pass your openBookDetails function as props from Update to Table then from Table to TableRow
You should send your functions to child component as props.
import React, { Component } from 'react';
import './Update.css';
import Search from '../Search/Search';
const Table = ({ data, action }) => (
<table class="table table-hover">
<thead>
<tr class="table-primary">
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">ISBN</th>
<th scope="col">No. Of Copies</th>
</tr>
</thead>
<tbody>
{data.map(row =>
<TableRow key={row.id} row={row} action={action} />
)}
</tbody>
</table>
)
const TableRow = ({ row, action }) => (
<tr class="table-light" onClick={action()}>
<th scope="row" >{row.title}</th>
<td >{row.author}</td>
<td >{row.isbn}</td>
<td >24</td>
</tr>
)
class Update extends Component{
constructor(props) {
super(props);
this.state = {
value: '',
suggestions: [],
setOfAllBooks: [],
searchedBooks: []
};
this.openBookDetails = this.openBookDetails.bind(this);
this.setTableData = this.setTableData.bind(this);
}
setTableData(searchedBook){
this.setState({searchedBooks: searchedBook})
console.log(this.state.searchedBooks)
}
openBookDetails(){
console.log("openBookDetails")
}
render(){
return(
<div>
<Search state={this.state} setTableData={this.setTableData} />
<Table data={this.state.searchedBooks} action={this.openBookDetails} />
</div>
)
}
}
export default Update;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.