pure’ components code example
Example 1: pure components
import React from 'react';
import { withRouter } from 'react-router';
let globalHistory = null;
class Spy extends React.Component {
constructor(props) {
super(props)
globalHistory = props.history;
}
componentDidUpdate() {
globalHistory = this.props.history;
}
render(){
return null;
}
}
export const GlobalHistory = withRouter(Spy);
export default function getHistory() {
return globalHistory;
}
Example 2: pure components
import { useHistory } from "react-router-dom";
function HomeButton() {
const history = useHistory();
function handleClick() {
history.push("/home");
}
return (
<button type="button" onClick={handleClick}>
Go home
</button>
);
}