0

I have react app with a react-spreadsheet component and a button. I am trying to get the data from the spreadsheet on the onclick() event of the button.

import * as React from 'react';
import PropTypes from 'prop-types';
import Tabs from '@mui/material/Tabs';
import Tab from '@mui/material/Tab';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import { TextField, Button } from '@mui/material';
import './App.css';

import Spreadsheet from "react-spreadsheet";

import Dropdown from 'react-dropdown';
import 'react-dropdown/style.css';

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return (
    <div
      role="tabpanel"
      hidden={value !== index}
      id={`simple-tabpanel-${index}`}
      aria-labelledby={`simple-tab-${index}`}
      {...other}
    >
      {value === index && (
        <Box sx={{ p: 3 }}>
          <Typography>{children}</Typography>
        </Box>
      )}
    </div>
  );
}


function loadBatchInput() {
    alert("inside");
}

function App() {
  const [tabValue, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  //batch input excel
  const [batchInputData, setbatchInputData] = React.useState([
    [{ value: "Type"}, { value: "Region" }, { value: "Product" }, { value: "Brand" }, { value: "Sku_Type" }],
    []
  ]);


  return (
    <div className="App">
      <div className="content">
          <TabPanel value={tabValue} index={0}>
            <div className='rowA'>
              <div>
                Batch Input Table
              </div>
              &nbsp;&nbsp;&nbsp;&nbsp;
              <div>
                <Button variant="contained" onClick={() => loadBatchInput() }>Load Batch Input</Button>
              </div> 
              &nbsp;&nbsp;&nbsp;&nbsp;              
              <div>
                <Spreadsheet data={batchInputData} onChange={setbatchInputData} />
              </div>
            </div>
          </TabPanel>
      </div>
    </div>
  );
}

export default App;

I am trying to get the data added to the spreadsheet via the webpage inside a JavaScript function.

Is there a way to get the react-spreadsheet data(if any added via frontend) inside the loadBatchInput() on click of the Load Batch Input button

Thanks,

1 Answer 1

1

In your loadBatchInput() function, you will need to add your data to the 2nd element of the array of your batchInputData state. You can do so with the following:

  const loadBatchInput = () => {
    let batch = [...batchInputData];
    batch[1] = [{value: 'data'}]
    setbatchInputData(batch);
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Not exactly what i was looking for.. but it did gave me how to proceed further, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.