簡體   English   中英

Append 文本輸入到 URL 在 React

[英]Append Text Input to URL in React

我正在嘗試 append 一個文本值到以下“http://3-10.cetecerpdevel.com:3101/customer/list_new”中來自“?external_key=blabla”的TextFilter。 所以換句話說,http://3-10.cetecerpdevel.com:3101/customer/list_new?external_key=blabla。 我看過做 react-router 但它似乎不適合我正在做的事情,我嘗試了 URLSearchParams。 下面的代碼是我正在使用的代碼片段。 你會怎么做?

function OnChange(text){
        if (text === null) {
            console.log("Do nothing");
        } else {
            //append to url
            console.log("Hi")
            let url = new URL('https://example.com?foo=1&bar=2');

            let test = document.getElementById('ExternalKey');
            // just append a word to the url
            const blah = "blah";
            
            console.log("test",test);
        }
    }   

...

<TextFilter
                                label="External Key"
                                urlText={OnChange()}
                                id="ExternalKey"
                                placeholder="Search External Key"
                                dataFieldName="me.external_key"
                                divClass="form-field column third"
                            /

只需您可以使用useHistory來執行此操作:

const history = useHistory();

history.push({
      pathname: "/",// Current Path
      search: "?color=blue"// Your Param needed
    });

搜索中的任何內容都將 append 到 url...

function OnChange(text){
        if (text === null) {
            console.log("Do nothing");
        } else {
            history.push({
  pathname: "/",// Current Path
  search: "?foo=1&bar=2"// Your Param needed
});
        }
    }   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM