linkButton: refactor using React Hooks

This commit is contained in:
Peter Cai 2020-02-20 10:24:10 +08:00
parent 8c81b9e66f
commit a5d0d58fef
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
1 changed files with 13 additions and 19 deletions

View File

@ -1,23 +1,17 @@
import React from "react"
import React, { useState } from "react"
import { Redirect } from "react-router-dom"
# A replacement of react Link that uses a button
class LinkButton extends React.Component
constructor: (props) ->
super props
@state =
switch: false
export default LinkButton = (props) ->
[jump, setJump] = useState false
render: ->
# We cannot just replace the button with a <Redirect/> when we switch
# because it will cause visual breaks where the button disappears for
# a short while before the actual switch happens.
<React.Fragment>
<button {...@props} onClick={(e) => @setState { switch: true }}/>
{
@state.switch and
<Redirect {...@props} />
}
</React.Fragment>
export default LinkButton
# We cannot just replace the button with a <Redirect/> when we switch
# because it will cause visual breaks where the button disappears for
# a short while before the actual switch happens.
<React.Fragment>
<button {...props} onClick={(e) => setJump true}/>
{
jump and
<Redirect {...props} />
}
</React.Fragment>