试图找出在React无状态组件中创建事件处理程序的最佳方法.我可以这样做:
const myComponent = (props) => {
const myHandler = (e) => props.dispatch(something());
return (
<button onClick={myHandler}>Click Me</button>
);
}
Run Code Online (Sandbox Code Playgroud)
这里的缺点是,每次渲染此组件时,都会创建一个新的"myHandler"函数.有没有更好的方法在仍然可以访问组件属性的无状态组件中创建事件处理程序?
我一直在寻找normalizr来展平以标准JSON API格式格式化的JSON API数据.谁能指点我这样做的一些例子?我特别想知道如何处理资源对象关系的normalizr模式(由JSON API标准定义).在JSON API标准中,在资源对象中定义了"relationships"属性,然后是每组相关对象的属性.以下是JSON API格式的单个产品类别的示例,其中包含两个相关产品:
{
"jsonapi": {
"version": "1.0"
},
"meta": {
"time": "0.006"
},
"data": [
{
"type": "category",
"id": "6",
"attributes": {
"name": "Odwalla"
},
"meta": {
"product_count": "0"
},
"relationships": {
"product": {
"data": [
{
"type": "product",
"id": "4785"
},
{
"type": "product",
"id": "4786"
}
]
}
}
}
],
"included": [
{
"type": "product",
"id": "4786",
"attributes": {
"name": "Strawberry & Banana Odwalla",
"description": null,
"price": "3.19",
"upc": …Run Code Online (Sandbox Code Playgroud)