I have a react component which looks like this.
import React, { PropTypes, Component } from 'react';
import { Accordion, Panel, PanelGroup, Table } from 'react-bootstrap';
const FormCell = ({ data }) => (
<div>
<a className="pdf-name" onClick={this.doSomething}>{data.item.extension.nameCodeDes}</a>
</div>
);
class Docs extends Component {
constructor(props) {
super(props);
this.doSomething= this.doSomething.bind(this);
}
doSomething() {
setTimeout(() => {
console.log("here we are");
})
}
// ...
}
Docs.defaultProps = {
tableData: null,
cols: null,
metaData: {
documentsColumnMetaData: [
{
displayName: 'Policy/Account',
columnComponent: {
component: FormCell,
actions: [],
},
},
],
},
};
export default Docs;
The this.doSomething is transpiled to undefined.doSomething in dev tools. I get an error Cannot read property 'downloadDocument' of undefined. Can someone please let me know what I'm missing here? P.S FormCell does more that what is posted. I reduced the code for simplicity
This looks like you're doing it wrong but I'll still answer your question directly. The issue here is that you are defining
FormCell
outside ofDocs
but you expectthis
to be an instance ofDocs
. What you can do ischange it to:
And then I assume that you omitted some code inside the
Docs
component that looks like thisNotice how I added the
doSomething={ this.doSomething }
. That will allow you to pass thedoSomething
method to theFormCell
component as a prop.