1

I am struggling with the hooks and how can i pass the value from one component to another. I have a component called table.js from which i am trying to pass the table cell clicked value to Grid.js. The variable is peerData . I am able to print it on console but can't render it in grid, also not able to use the ag grid api call and getting undefined. Can someone please help me, i am very new to javascript world.

Grid.js :

import ..
   
    const { selectedCompany, peerData } = React.useContext(Context);
    
    
    React.useEffect(() => {
      console.log({peerData})
    }, [peerData]);
    
    
    
    console.log('Checking Context..');
    console.log(selectedCompany);
    console.log({'peerdata detail':peerData});
    
    
    const columnDefs = [
      {
        id: 'currency',
        field: 'currency',
        headerName: 'currency',
      },
      {
        id: 'Cash',
        field: 'Cash',
        headerName: 'Cash',
        width : 200,
        resizable : true,
      },
    ];
    
    
    
    const getPeerData = (rows) => {
      let company = rows;
      console.log('Peer Company Name:')
      console.log(company)
      return company;
    };
    
    const gridOptionsPeers = {
      columnDefs : columnDefs,
      rowData : getPeerData(peerData),
      rowSelection : 'multiple'
    };
    
    
    
    export default function PeerDetails(props) {
    
    
      const { selectCompany } = React.useContext(Context);
    
      function onSelectionChanged() {
        console.log({'Printing Peer Grid':gridOptionsPeers})
        var selectedRows = gridOptionsPeers.api.getSelectedRows();
        if (selectedRows.length == 1) {
          const fsymId = selectedRows[0].currency;
          selectCompany(id);
          console.log({'These rows Selected' : id})
        }
        else {
          console.log('No row selected');
        }
      }
      return (
        <Card>
          <CardHeader><h4>Peers</h4>
          </CardHeader>
          <CardBody>
            <Row style={{ height: '450px' }}>
              {/* {  <Col w='' style={{ width: w, minWidth: w, maxWidth: w }}>
                {<PeersChart />}
              </Col>} */}
              <Col>
                <Grid
                  columnDefs={columnDefs}
                  style={{ width: '100%', height: '100%' }}
                  containerStyle={{ width: '100%', height: '100%' }}
                  tooltipShowDelay={0}
                  onSelectionChanged={onSelectionChanged}
                  gridOptions={gridOptionsPeers}
                />
              </Col>
            </Row>
          </CardBody>
        </Card>
      );
    };
Shivam Agrawal
  • 2,053
  • 4
  • 26
  • 42
  • Your hooks (`useContext` and `useEffect`) are meant to be within the functional component – Phil Sep 20 '21 at 05:33
  • @Phil I tried something like this : const Allowance = () => { const { selectedCompany, peerData } = React.useContext(Context); But then got the error Parsing error: 'import' and 'export' may only appear at the top level. – Shivam Agrawal Sep 20 '21 at 05:42
  • 1
    Is your `React.useEffect` outside the functional component? That won't work. I see that the file name you have is grid.js but you are exporting PeerData component. I think I am little confused. If you have two components, please write two code snippets. – Jayendra Sharan Sep 20 '21 at 06:01

0 Answers0