Ad
React Custom Component(DataTable)
im new to react and im trying to build a custom componenet with parameters.
Im just wonding whats the exact way on doing it.
Here is my current code, how should i pass those Columns
,ajax
and datasource
to the componenet.
Or im i doing it wrong?
import * as React from 'react';
interface Column {
name: string,
header: string,
value: Function
}
export default class DataTable extends React.PureComponent<({
dataSource: any[],
ajax: string
columns: Column[]
onEdit: Function,
onDelete: Function
})>{
public state = {
dataSource: [],
ajax: undefined,
columns: [],
onEdit: undefined,
onDelete: undefined
}
componentDidMount() {
if (this.state.ajax != undefined) {
fetch(this.state.ajax)
.then(response => response.json())
.then(data => this.setState({ dataSource: data }));
}
}
render() {
var template = (
<table className="table">
<thead className="thead-darked">
{
this.state.columns.map((x: Column) => {
<th scope="col">{x.header}</th>
})
}
</thead>
</table>)
return template;
}
}
Ad
Answer
- You don't need
ajax
,columns
,onEdit
, andonDelete
state. Just use the props. - Initialize
this.state.dataSource
withthis.props.dataSource
.
Two changes above will resolve your problem. Additionally,
- You may want to fetch the data again if
ajax
prop changes. You have to implementcomponentDidUpdate
method for the behavior. However, I recommend making a function component and usinguseEffect
hook. - Your
dataSource
state won't be updated ifdataSource
prop changes. It can lead to a buggy behavior though you can add more code to avoid bugs. If you move the "ajax" part outside of the component and only usedataSource
prop, the logic will be clearer and less bug-prone.
Below is the updated code.
interface Props {
dataSource: any[],
ajax: string,
columns: Column[],
onEdit: Function,
onDelete: Function
}
export default class DataTable extends React.PureComponent<Props>{
public state = {
dataSource: this.props.dataSource,
}
componentDidMount() {
if (this.props.ajax != undefined) {
fetch(this.props.ajax)
.then(response => response.json())
.then(data => this.setState({ dataSource: data }));
}
}
render() {
const template = (
<table className="table">
<thead className="thead-darked">
{
this.props.columns.map((x: Column) => {
<th scope="col">{x.header}</th>
})
}
</thead>
{/* render something with this.state.dataSource */}
</table>
);
return template;
}
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad