1

I have parent component which showing records in table. When user click it redirect to child component with record Id passing in parameter using history push. This is all working fine but I need to pass an object of data in addition to parameter which I am not able to achieve and also what steps I need in child component to read this object?

Object Definition

export interface ISearchCriteriaForm{
  startTime: string,
  endTime: string,
  liveTime?: string,
  schedAction_Active: string,
  siteId?: number,
  scheduleId?: number
}

Parent Component

const MyComponentA = () => {

 const [url] = useState("/eziTracker/eziStatus");
const[eziSearchCriteria, setEziSearchCriteria] = useState<ISearchCriteriaForm>();

//when row clicked on table
const selectedRow = (row: any) => { 
//history.push(`${url}/${row.siteId}`);  // this works
 
 history.push({     // this is how I am trying to achieve ??
   pathname: `${url}/${row.siteId}`,
   state: eziStatusSearchCriteria
  });
 };

Child Component

const MyChildComponent = () =>{
  const match = useRouteMatch("/eziTracker/eziStatus/:id/:hash");
  const[eziStatusSearchCriteria, setEziStatusSearchCriteria] = useState<IEziStatusSearchCriteriaForm>();

  useEffect(() =>{
  
  },[]);

error

Expression expected.  TS1109

   88 |       pathname: `${url}/${row.siteId}`,
   89 |       state: eziStatusSearchCriteria
 > 90 |     });
      |     ^
   91 | };
   92 | 
   93 | const setDefaultSearchCriteria = () =>{
3
  • I think this is not an issue in typescript issue instead of rect.js error. I think you should assign { // this is how I am trying to achieve ?? pathname: ${url}/${row.siteId}, state: eziStatusSearchCriteria } state to variable and call it again in the history.push method as 2 statements. like, stackoverflow.com/questions/35821614/… Commented Jan 3, 2021 at 7:21
  • Do you want filter table data using the query parameter? Commented Jan 3, 2021 at 7:24
  • You are right, I have change the way I assign value to state and it did work, refer to my asnswer Commented Jan 3, 2021 at 7:31

2 Answers 2

2

Usually you need to do something like this to achieve you're goal:

first you need to create a location object for history.push function like this:

const location = {
   pathname: `${url}/${row.siteId}`,
   state: eziStatusSearchCriteria || {}
}

and then use history.push(location) to go to your specified location and then in the destination all you need to do is to some how access location object which the easiest way is to use useLocation hook like this:

const location = useLocation();

//NOW you can use location.state object to get your data
Sign up to request clarification or add additional context in comments.

1 Comment

@Toxic Glad I could help ;)
0

Parent component

const selectedRow = (row: any) => { 

eziSearchCriteria.siteId = row.siteId;

history.push({
  pathname: `${url}/${row.siteId}`,
  state: {searchCriteria: eziSearchCriteria}
});
};

Child Component

  let location = useLocation();

  const setSearchCriteria =() =>{
   if(location.state.searchCriteria!=null || location.state.searchCriteria !=''){
     setEziStatusSearchCriteria(location.state.searchCriteria)
   }
 }

Comments

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.