我想知道处理我的情况的最佳方法是什么。我有两个{
"aggregations": {
"mainAggregation": {
"buckets": [
{
"key": 17572,
"doc_count": 30,
"available_stock": {
"value": 24000
}
},
{
"key": 17598,
"doc_count": 10,
"available_stock": {
"value": 12000
}
},
{
"key": 17602,
"doc_count": 8,
"available_stock": {
"value": 6000
}
}
]
}
}
}
组件,每个组件都将状态中的不同变量传递给下一个组件。根据传递的状态,Component有条件地呈现页面上的某些元素。
最佳做法是获取位置的状态并设置Component的状态以保存该变量?或者可以不将其保存在Component的状态,并根据位置的状态有条件地渲染?
无论我采取哪种方式,但我想知道你的想法。
链接示例:
Link
保存到州的组件示例:
<NavLink
key='individual'
to={{
pathname: '/',
state: 'individual',
}}>
{name}
</NavLink>
<NavLink
key='business'
to={{
pathname: '/',
state: 'business',
}}>
{name}
</NavLink>
组件示例未保存到州:
export default ({
location,
customer
}) => {
if (customer !== location.state.type) updateCustomerType(location.state.type)
return (
{customer === 'individual'
? ( <div> Hello! </div> )
: ( <div> World! </div> )
}
)
}
使用React Router 4&amp;重构