React Router Hook Functions

useParams

Hook function to access url parameters.

const Anecdote = ({ anecdotes }) => {
const id = useParams().id
const anecdote = anecdotes.find(quote => quote.id === id)
return (
<div>
<h3>{anecdote.content} by {anecdote.author}</h3>
<p>has {anecdote.votes} votes</p>
<p>for more info see <a href={anecdote.info}>{anecdote.info}</a></p>
</div>
)
}

useHistory

Hook function to access the browser's history object to change the url.

const CreateNew = (props) => {
const [content, setContent] = useState('')
const [author, setAuthor] = useState('')
const [info, setInfo] = useState('')
const history = useHistory()
const handleSubmit = (e) => {
e.preventDefault()
props.addNew({
content,
author,
info,
votes: 0
})
history.push('/')
}
// ...