0

I am trying to redirect from Page 'SalesList.jsx' to 'SalesInvoice.jsx' by clicking a button, but failed to do so. I am getting all the props on button click but unable to call the later page.

I am sharing my code for better understanding:

  1. calling onSalesView function on button click by passing row value as props
<Table.Row key={id}>
  <Table.Cell>{row.id}</Table.Cell>
  <Table.Cell>{row.ServiceNo}</Table.Cell>
  <Table.Cell>{row.ServiceName}</Table.Cell>
  <Table.Cell>{row.Cost}</Table.Cell>
  <Table.Cell>
    <Dropdown icon='ellipsis vertical' floating direction="left" className='icon'>
      <Dropdown.Menu>
        <Dropdown.Item onClick={()=> onSalesView(row) }>Sales Invoice</Dropdown.Item>
      </Dropdown.Menu>
    </Dropdown>
  </Table.Cell>    
</Table.Row>
  1. Trying to redirect to the new page with table values. Table values are coming properly, But couldn't accomplish the redirection part.
const onSalesView = (data) => {
   return <SalesInvoice data={data} />  
}
9
  • Does this answer your question? What is the best way to redirect a page using React Router? Commented Jan 9, 2022 at 21:02
  • No, but thanks @enzo. Commented Jan 9, 2022 at 21:08
  • are you using a router? Commented Jan 9, 2022 at 21:17
  • No Stefan. Can we use router dom while passing any props? Commented Jan 9, 2022 at 21:20
  • you can pass parameters, otherwise i wold use redux.. Commented Jan 9, 2022 at 21:22

1 Answer 1

1

What you did is returning jsx component that will show in the Dom ... and not redirect to other component

You can use history API ... and also you can pass data to the other component like this

import { useHistory } from "react-router-dom";

const FirstPage = props => {
    let history = useHistory();

    const someEventHandler = event => {
       history.push({
           pathname: '/secondpage',
           search: '?query=abc',
           state: { detail: 'some_value' }
       });
    };

};

export default FirstPage;

========================= Get Sent Params ===========================
import { useEffect } from "react";
import { useLocation } from "react-router-dom";

const SecondPage = props => {
    const location = useLocation();

    useEffect(() => {
       console.log(location.pathname); // result: '/secondpage'
       console.log(location.search); // result: '?query=abc'
       console.log(location.state.detail); // result: 'some_value'
    }, [location]);

};


import { useHistory } from 'react-router-dom';

const YourComponent = () => {
...
const history = useHistory();
return {
    ...
    rows.map((row) => (
        <TableRow key={row.id} onClick={() => history.push(yourLocation)}>
          <TableCell component="th" scope="row">
            {row.id}
          </TableCell>
          <TableCell align="right">{row.name}</TableCell>
          <TableCell align="right">{row.calories}</TableCell>
          <TableCell align="right">{row.fat}</TableCell>
          <TableCell align="right">{row.carbs}</TableCell>
          <TableCell align="right">{row.protein}</TableCell>
        </TableRow>
      ))}
}


Sign up to request clarification or add additional context in comments.

2 Comments

Can i pass the row parameters into history.push?
Yes you can, I edited the answer above

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.