Home > OS >  How to get data in class A from class B in reactjs
How to get data in class A from class B in reactjs

Time:05-19

Here I added two Classes and want to get data from one class to another class but I am getting errors like this "Warning: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it."

import React, { Component } from 'react';
import DataHtml from "../datahtml";


export default class AllData extends Component {
    render(){
        return (
            <div className="row g-6 g-xl-9">
                <DataHtml />
            </div>
        );
    }
}


import React, { Component } from 'react';

class DataHtml extends Component {
    constructor(props) {
        super(props)

        this.state = {
            data: [ {"name": "demo","date": "02-02-2022"},{"name": "demo","date": "02-02-2022"}]        
            }
    }

    DataHtmlStructure = () => {
        this.state.data.map((data) => {
            return (
                <div className="col-md-6 col-xl-4 test-card">
                    <div className="card-body p-9 pt-4">
                        <div className="fs-3 fw-bolder text-dark mb-1">{data.name}</div>
                        <div className="fs-5 text-gray-600">
                            <i className="bi bi-calendar-date-fill"></i>{data.date}
                        </div>
                    </div>
                </div>
            );
        })
    }

    render() {
        return this.DataHtmlStructure;
    }
}
export default DataHtml;

CodePudding user response:

You need to invoke your function in your render method.

    render() {
        return this.DataHtmlStructure();
    }
  • Related