我的组件中有一个搜索栏。我想点击搜索,它应该调用搜索操作并将结果导航到父组件。
<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"。
谁能帮我这个?
const TextForm: React.FunctionComponent<Props> = (props) => {
const formError = yup.object({
name: yup.string().required("Required"),
});
const formValidation = (fieldName) => {
return {
invalid: !!form.errors[fieldName] && form.touched[fieldName],
invalidText: form.errors[fieldName],
onBlur: form.handleBlur,
};
};
const form = useFormik({
initialValues: {
name,
id,
},
formValidation
formError,
validateOnChange: true,
validateOnMount: true,
initialTouched: {},
});
return(
<React.Fragment>
<form>
<TextInput
id="text-input-2"
{...validation("name")}
type="text"
name = "name"
onChange={(event) => {
setName(event.target.value);
form.handleChange(event);
}}
/>
<TextInput
id="text-input-2"
{...validation("id")}
type="text"
name = "id"
onChange={(event) => {
setId(event.target.value);
}}
/> …Run Code Online (Sandbox Code Playgroud)