kar*_*017 3 reactjs recompose react-router-v4
我有这个按钮并使用recompose中的StateHandlers进行操作。目标只是将按钮状态从 active: false更改为 active: true。
const EnhancedButton = withStateHandlers(
({ initialState = false }) => ({
active: initialState
}),
{
onTrigger: ({ active }) => ({
active: true
})
}
)(({ active, onTrigger, children, id }) => {
console.log(active)
return (
<Button
onClick={() => onTrigger()}
toggle
active={active}
size="massive"
style={styles.button}
as={Link}
to={`/${id}`}
>
{children}
</Button>
)
})
Run Code Online (Sandbox Code Playgroud)
我点击按钮,然后我被重定向到新页面,然后我返回,按钮仍然处于活动状态: false我希望它处于 活动状态: true
withStateHandlers(
initialState: Object | (props: Object) => any,
stateUpdaters: {
[key: string]: (state:Object, props:Object) => (...payload: any[]) => Object
}
)
Run Code Online (Sandbox Code Playgroud)
这意味着每个状态更新器属性都是一个获取参数并返回另一个函数的函数state,props该函数又接受可选的有效负载参数(即您在调用时作为参数传递的任何内容onTrigger)并返回新状态。
您onTrigger返回新状态而不是函数,因此类型不正确。如果将结果包装在箭头函数中,它应该可以工作:
onTrigger: ({ active }) => () => ({
active: true
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2291 次 |
| 最近记录: |