pure components react code example
Example 1: react with pure components
Simple example of react pure component
import React from 'react';
class PercentageStat extends React.PureComponent {
render() {
const { label, score = 0, total = Math.max(1, score) } = this.props;
return (
<div>
<h6>{ label }</h6>
<span>{ Math.round(score / total * 100) }%</span>
</div>
)
}
}
Example 2: pure components
import { push } from 'react-router-redux'
dispatch(push('/some/path'))
Example 3: pure components
import { browserHistory } from 'react-router';
browserHistory.push('/some/path');
Example 4: 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 5: pure components
import { createBrowserHistory } from 'history'
export default createBrowserHistory({
})
Example 6: pure components
import { withRouter } from 'react-router-dom';
class MyComponent extends React.Component {
render () {
this.props.history;
}
}
withRouter(MyComponent);