Guest User

Untitled

a guest
Jun 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. class Login extends React.Component {
  2. state = {
  3. redirectToReferrer: false
  4. };
  5.  
  6. login = () => {
  7. fakeAuth.authenticate(() => {
  8. this.setState({ redirectToReferrer: true });
  9. });
  10. };
  11.  
  12. render() {
  13. const { from } = this.props.location.state
  14. || { from: { pathname: "/" } };
  15. const { redirectToReferrer } = this.state;
  16.  
  17. if (redirectToReferrer) {
  18. return <Redirect to={from} />;
  19. }
  20.  
  21. return (
  22. <div>
  23. <p>You must log in to view the page at {from.pathname}</p>
  24. <button onClick={this.login}>Log in</button>
  25. </div>
  26. );
  27. }
  28. }
  29.  
  30. class Login extends React.Component {
  31. constructor(){
  32. super();
  33. this.state = {
  34. redirectToReferrer: false
  35. };
  36. }
  37.  
  38. ....
  39.  
  40. }
  41.  
  42. // BookContainer.js
  43. import { Container } from 'unstated'
  44. class BookContainer extends Container {
  45. state = {
  46. books: [],
  47. booksVisible: false
  48. }
  49. addBook = book => {
  50. const books = [...this.state.books, book]
  51. this.setState({ books })
  52. }
  53. toggleVisibility = () => {
  54. this.setState({
  55. booksVisible: !this.state.booksVisible
  56. })
  57. }
  58. }
  59. export {
  60. BookContainer
  61. }
Add Comment
Please, Sign In to add comment