how to access array datat in class component react

Solutions on MaxInterview for how to access array datat in class component react by the best coders in the world

showing results for - "how to access array datat in class component react"
Gianluca
10 Oct 2019
1
2
3import React, { Component } from 'react'
4import './TourList.scss';
5import Tour  from '../Tour/Tour';
6import { tourData } from './tourData'; //impoert array of data
7export default class TourList extends Component {
8    state={
9        tours:tourData // store array of data in state
10    }
11    render() {
12        const {tours}=this.state // store  state in variable
13        
14
15        return (
16            <section className="toulist">
17            {tours.map(tour=>{ // using map method to destructure
18                return <Tour key={tour.id} tour={tour} />; //pass as props
19            })}
20            
21            </section>
22        )
23    }
24}
25//----------------------------------------------------
26 //how to access props in class component
27   const {id,city}=this.props.tour//props