[英]Is there a way to set input placeholder as props in react?
I'm intending to have different values for input placeholder as I route to different pages.当我路由到不同的页面时,我打算为输入占位符设置不同的值。 Is it possible to set an attribute as a props?
是否可以将属性设置为道具?
Below is my code for Search module or component, where the placeholder I'd like to use as props exist.下面是我的搜索模块或组件的代码,其中存在我想用作道具的占位符。
export default function Search(props) {
return (
<div className={styles.container}>
<FontAwesomeIcon icon={faSearch} />
<input type="text" placeholder={props.search} />
</div>
);
}
This Search component is used in another component before it's rendered in App.js.此 Search 组件在 App.js 中呈现之前在另一个组件中使用。
export default function Page({ data, setModalVisibleState, }) {
const addButtonHandler = () => {
setModalVisibleState(true);
};
return (
<div className={styles.container}>
<div className={styles.titleBox}>
<FontAwesomeIcon icon={faGraduationCap} />
<p>{data.title}</p>
<Button size="normal" shape="squared" color="blue" onClick={addButtonHandler}>{data.button}</Button>
</div>
<div className={styles.menuBox}>
<ul>
{data.menu.map((v, i) => (
<li key={i}>
<BodaSelect name={v} />
</li>
))}
</ul>
<Search search={search} />
</div>
...
Finally, this is the App.js where I'd like to set the value for placeholder as I route each page.最后,这是我想在路由每个页面时设置占位符值的 App.js。
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
function App() {
const [modalVisibleState, setModalVisibleState] = useState(false);
return (
<div className="App">
<Router>
<Sidebar />
{modalVisibleState ? (
<Modal setModalVisibleState={setModalVisibleState} />
) : (
""
)}
<Switch>
<Route path="/assignment">
<Page
search="search assignments"
data={{
menu: ["class A"],
title: "assignment",
button: "Add a new assignment",
table: ["Date", "Textbook", "Content"],
tableData: [
["2021/03/31",
"English 101",
"Assignment guideline",
],
["2021/03/28",
"Maths 101",
"Assignment details",
],
],
}}
setModalVisibleState={setModalVisibleState}
/>
</Route>
<Route path="/">
<Page
search="search students"
data={{
menu: ["All schools", "All grades", "All classes", "All homeroom teachers"],
title: "student",
button: "Add a new student",
table: ["Students", "Grades", "Schools", "Homeroom teachers", "Classes"],
tableData: [
[
"Lisa Parker",
"12",
"Orange High School",
"Kate Lee",
"Orange Juice Class · Orange Farming Class · Mango Class",
],
],
}}
setModalVisibleState={setModalVisibleState}
/>
</Route>
</Switch>
</Router>
</div>
);
}
If it's not possible to set placeholder as props, would there be a way to set placeholder value variable?如果无法将占位符设置为道具,是否有办法设置占位符值变量?
Thank you for your help.谢谢您的帮助。
yes.是的。 It is possible to set placeholders as props.
可以将占位符设置为道具。 Everything you do fine.
你做的一切都很好。 But I saw you don't declare props `search in Page component.
但是我看到您没有在 Page 组件中声明 props `search。 You need to add this props:
你需要添加这个道具:
export default function Page({ data, setModalVisibleState, search}) {...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.