在下一页中搜索结果 reactjs

Sin*_*ngh 3 javascript routes input typescript reactjs

我的组件中有一个搜索栏。我想点击搜索,它应该调用搜索操作并将结果导航到父组件。

 <input 
              onKeyPress={(event) => {
             if (event.key === "Enter") {
                  this.onSearch(event.target.value);
                }
              }}
              type="text"
            />
Run Code Online (Sandbox Code Playgroud)

方法:

 onSearch(searchString) {
     // perform Search
     this.props.history.push("/details");
  } 
Run Code Online (Sandbox Code Playgroud)

我希望它导航到搜索字符串详细信息页面,在URL我期待是这样的: http://localhost:8080/details?search="searchString"

谁能帮我这个?

Dan*_*cci 6

你可以这样做

onSearch(searchString) {
  // perform Search
  this.props.history.push("/details?search=" + encodeURIComponent(searchString));
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。