reactjs - React 和 TypeScript 如何通过道具在 onClick 上将数据从子级传递给父级
问题描述
我有父子组件。而且我不想通过道具在 onClick 上将子组件数据传递给父组件。我怎样才能做到这一点?我试过了:
父.tsx
const items = [
{
title: "A"
},
{
title: "B"
}
]
const Parent = () => {
const [someState, setSomeState] = useState("");
const toggleState = (e: React.MouseEvent, title: string) => {
setSomeState(title);
}
return (
<Child items={items} toggleState={(e) => toggleState(e, item.title)} />
)
}
孩子.tsx
type ChildProps = {
items: Item[];
toggleState: (e: React.MouseEvent) => void;
}
const Child: React.FC<ChildProps> = (props) => {
return (
{props.items.map((item) => (
<button onClick={props.toggleState}> pass clicked item title to parent </button>
))}
)
}
但在我的父组件omn toggleState prop item.title 中显示错误:
任何找不到名称“项目”。
解决方案
与任何其他函数调用一样,您必须将信息作为参数传递:
Parent.tsx
:
const Parent = () => {
const [someState, setSomeState] = useState("");
const toggleState = (e: React.MouseEvent, title: string) => {
setSomeState(title);
}
return (
<Child toggleState={(e, title) => toggleState(e, title)} items={items}/>
)
// −−−−−−−−−−−−−−−−−−−−−^^^^^^^−−−−−−−−−−−−−−−−−−−−^^^^^
}
Child.tsx
:
type ChildProps = {
items: Item[];
toggleState: (e: React.MouseEvent, title: string) => void;
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^
}
const Child: React.FC<ChildProps> = (props) => {
return (
{props.items.map((item) => (
<button onClick={(e) => props.toggleState(e, item.title)}> pass clicked item title to parent </button>
))}
)
// −−−−−−−−−−−−−−−−−−^^^^^^^−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^
}
注意toggleState
inParent.tsx
和onClick
in的变化Child.tsx
。
现场示例:
const { useState } = React;
/*
interface Item {
title: string;
}
*/
const items = [
{title: "Item 1"},
{title: "Item 2"},
{title: "Item 3"},
{title: "Item 4"},
];
const Parent = () => {
const [someState, setSomeState] = useState("");
const toggleState = (e/*: React.MouseEvent*/, title/*: string*/) => {
setSomeState(title);
}
return (
<div>
<div>someState = {someState}</div>
<Child items={items} toggleState={(e, title) => toggleState(e, title)} />
</div>
)
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^−−−−−−−−−−−−−−−−−−−−^^^^^
}
/*
type ChildProps = {
items: Item[];
toggleState: (e: React.MouseEvent, title: string) => void;
}
*/
const Child/*: React.FC<ChildProps>*/ = (props) => {
return <React.Fragment>
{props.items.map((item) => (
<button onClick={(e) => props.toggleState(e, item.title)}> pass clicked item title to parent </button>
))}
</React.Fragment>
// −−−−−−−−−−−−−−−−−−^^^^^^^−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^
}
ReactDOM.render(<Parent />, document.getElementById("root"));
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.12.0/umd/react-dom.production.min.js"></script>
在操场上展示类型的工作。
推荐阅读
- c# - 依赖注入 - HttpClient 和 OAuth2
- python - 在python的文本小部件中插入文本的问题
- c# - 重复作业不会在设定的时间被调用
- php - 我如何知道命令执行是否完成并在发生错误时通知?
- python - 如果并行运行 Pytest,则跳过/排除测试模块
- javascript - 如何以毫秒为单位将时间戳转换为 javascript?(在反应中)
- selenium - Python selenium 等到不等搜索出现
- google-sheets - 如何在 googlesheet 公式中编写动态工作表引用?
- dart - 单个变量的多种类型(参数/返回类型)
- angular - Angular:如何根据父应用程序组件的路由更改重新渲染子组件?