[英]Can't change the state withStateHandlers from recompose
I have this button and playing around withStateHandlers from recompose . 我有这个按钮,并通过recompose玩withStateHandlers 。 The goal is just to change button state from active: false to active: true .
目标只是将按钮状态从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>
)
})
I click on the button, then I get redirected to new page, then I go back and the button is still active: false where I expect it to be active: true 我单击按钮,然后将我重定向到新页面,然后返回,该按钮仍处于活动状态:false我希望它处于活动状态:true
The docs for withStateHandlers specify: withStateHandlers的文档指定:
withStateHandlers(
initialState: Object | (props: Object) => any,
stateUpdaters: {
[key: string]: (state:Object, props:Object) => (...payload: any[]) => Object
}
)
Which means that each state-updater property is a function that gets state
and props
arguments and returns another function, which in turn takes the optional payload arguments (ie whatever you pass as arguments when you call onTrigger
) and returns the new state. 这意味着每个国家更新属性是得到一个功能
state
和props
参数和返回另一个功能,这又需要可选的有效载荷参数(即无论你作为参数传递,当你调用onTrigger
)并返回新的状态。
Your onTrigger
returns the new state instead of a function, so the type is incorrect. 您的
onTrigger
返回新状态而不是函数,因此类型不正确。 If you wrap the result in an arrow function, it should work: 如果将结果包装在箭头函数中,则应该可以使用:
onTrigger: ({ active }) => () => ({
active: true
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.