English 中文(简体)
我如何根据我在其构成部分中的地位来建立冷链联系
原标题:How do I create a url link depending on what state I have in my component

I am trying to create a query link that is created based on the state of the component. When user selects an option the states are changed and then user can select search button. The query on the search button should however be according to the states that were changed by the user. For eg.

/rent/?bedrooms=1&bathrooms=1
/rent/?bedrooms=2&parking=1
const FilterOption = () => {
  const [postCode, setPostCode] = useState();
  const [price, setPrice] = useState();
  const [bedroomCount, setBedroomCount] = useState(0);    
  const [bathroomCount, setBathroomCount] = useState(0);
  const [parkingCount, setParkingCount] = useState(0);
}
问题回答

不是问问一下,你可以使用反应路线国吗? 类似情况

const routeState = {
    postCode,
    price,
    bedroomCount,
    bathroomCount,
    parkingCount,
};
<Link to={{ pathname: "/your-destination-route", state: routeState }}>Go to Details</Link>

然后使用。

import { useLocation } from  react-router-dom ;
 const { postCode, price, bedroomCount, bathroomCount, parkingCount } = location.state || {};

使用<> 密码>useSearchParams hook to take the current state Value and Update the URL search params。

例:

import { useSearchParams } from  react-router-dom ;

const FilterOption = () => {
  const [searchParams, setSearchParams] = useSearchParams();

  const [postCode, setPostCode] = useState();
  const [price, setPrice] = useState();

  const [bedroomCount, setBedroomCount] = useState(
    Number(searchParams.get("bedrooms") ?? 0)
  );
  const [bathroomCount, setBathroomCount] = useState(
    Number(searchParams.get("bathrooms") ?? 0)
  );
  const [parkingCount, setParkingCount] = useState(
    Number(searchParams.get("parking") ?? 0)
  );

  const searchHandler = () => {
    setSearchParams(searchParams => {
      searchParams.set("bedrooms", bedroomCount);
      searchParams.set("bathrooms", bathroomCount);
      searchParams.set("parking", parkingCount);
      return searchParams;
    });
  };

  ...
}

如果你对搜索灯片需要更多控制,那么你可以检查点数,并在计数为零的地方清除碎片。

const searchHandler = () => {
  setSearchParams(searchParams => {
    if (bedroomCount) {
      searchParams.set("bedrooms", bedroomCount);
    } else {
      searchParams.delete("bedrooms");
    }
    if (bathroomCount) {
      searchParams.set("bathrooms", bathroomCount);
    } else {
      searchParams.delete("bathrooms");
    }
    if (parkingCount) {
      searchParams.set("parking", parkingCount);
    } else {
      searchParams.delete("parking");
    }

    return searchParams;
  });
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签