Answers for "pure components"

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>
  );
}
Posted by: Guest on February-15-2020
0

pure components

// some-other-file.js
import history from './history'
history.push('/go-here')
Posted by: Guest on April-29-2020
0

pure components

// then, in redux actions for example
import { push } from 'react-router-redux'

dispatch(push('/some/path'))
Posted by: Guest on April-29-2020
0

pure components

import getHistory from './history'; 

export const goToPage = () => (dispatch) => {
  dispatch({ type: GO_TO_SUCCESS_PAGE });
  getHistory().push('/success'); // at this point component probably has been mounted and we can safely get `history`
};
Posted by: Guest on April-29-2020
0

pure components

// history.js
import { createBrowserHistory } from 'history'

export default createBrowserHistory({
  /* pass a configuration object here if needed */
})
Posted by: Guest on April-29-2020
0

pure components

import { withRouter } from 'react-router-dom';

class MyComponent extends React.Component {
  render () {
    this.props.history;
  }
}

withRouter(MyComponent);
Posted by: Guest on April-29-2020
0

pure components

// index.js
import { Router } from 'react-router-dom'
import history from './history'
import App from './App'

ReactDOM.render((
  <Router history={history}>
    <App />
  </Router>
), holder)
Posted by: Guest on April-29-2020
0

pure components

import React from 'react';
import { withRouter } from 'react-router';

// variable which will point to react-router history
let globalHistory = null;

// component which we will mount on top of the app
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 react-router history
export default function getHistory() {    
  return globalHistory;
}
Posted by: Guest on April-29-2020
0

pure components

import { browserHistory } from 'react-router';
browserHistory.push('/some/path');
Posted by: Guest on April-29-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language