I have my table view.And i have posfields that are perfecting displaying by using map function.But my problem is when i'm trying to map td inside posfields map function its throwing me the error "'headers' of undefined".
{
this.POSFields.map(function (posFields, POSFieldsId) {
return (
<tr>
<td className="posheader" key={POSFieldsId} value={posFields.POSFieldsId}
{posFields.POSFields} </td>
<td>
<select className="selectpicker">
<option value="">Select Value</option>
{this.headers.map(function (headers) {
return (
<option key={headers}>{headers}</option>
);
})}
</select>
</td>
</tr>
)
})
}
In React, the map method is used to traverse and display a list of similar objects of a component. A map is not a feature of React. Instead, it is the standard JavaScript function that could be called on an array. The map() method creates a new array by calling a provided function on every element in the calling array.
Like @Andrew already suggested, you should use arrow
functions in order to be able to access this
within your map. You currently loose the context of this
{
this.POSFields.map((posFields, POSFieldsId) => {
return (
<tr>
<td className="posheader" key={POSFieldsId} value={posFields.POSFieldsId}
{posFields.POSFields} </td>
<td>
<select className="selectpicker">
<option value="">Select Value</option>
{this.headers.map((headers) => {
return (
<option key={headers}>{headers}</option>
);
})}
</select>
</td>
</tr>
)
})
}
Bind map function to have access to this
context:
{
this.POSFields.map(function (posFields, POSFieldsId) {
return (
<tr>
<td className="posheader" key={POSFieldsId} value={posFields.POSFieldsId}
{posFields.POSFields} </td>
<td>
<select className="selectpicker">
<option value="">Select Value</option>
{this.headers.map(function (headers) {
return (
<option key={headers}>{headers}</option>
);
})}
</select>
</td>
</tr>
)
}.bind(this))
}
Or the arrow functions this.POSFields.map((posFields, POSFieldsId) => {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With