Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Pure Component In React

import React from 'react';    
import PropTypes from 'prop-types';  
  
class Main extends React.Component {    
    constructor(props) {  
      super(props);          
      this.state = {  
         city: "Alwar",  
      }  
   } 
   
   componentDidMount(){
       setInterval(()=>{
           this.setState(()=>{
               return { city: "Alwar"}
           })
       },2000)
   }
   render() {  
       console.log('Main Component render');  
      return (  
          <div>    
         <h2>    
            {this.state.title}   
         </h2>    
         <p>User Name: {this.props.name}</p>  
         <p>User Age: {this.props.age}</p>  
         </div>  
      );    
   }  
     
}    
Main.propTypes ={  
       name:PropTypes.string.isRequired,  
       age:PropTypes.number.isRequired  
   }  
  
Main.defaultProps = {  
  name: 'Pankaj Kumar Choudhary',  
  age: 24  
};  
    
export default Main;
Comment

pure components

import { browserHistory } from 'react-router';
browserHistory.push('/some/path');
Comment

pure components

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

export default createBrowserHistory({
  /* pass a configuration object here if needed */
})
Comment

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)
Comment

pure components

// some-other-file.js
import history from './history'
history.push('/go-here')
Comment

pure components

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

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

withRouter(MyComponent);
Comment

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;
}
Comment

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`
};
Comment

pure components

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

dispatch(push('/some/path'))
Comment

PREVIOUS NEXT
Code Example
Javascript :: shadow react native generator 
Javascript :: v-bind shorthand 
Javascript :: tag name javascript 
Javascript :: hide html elements 
Javascript :: usestate callback 
Javascript :: checked unchecked through js 
Javascript :: bind method in javascript 
Javascript :: js contenteditable button spacebar 
Javascript :: set up emet for jsx in vs code 
Javascript :: Expresion regular para validar Dirección URL 
Javascript :: foreach in the elements with a data attibute jquery 
Javascript :: How to pass json format data on ajax call 
Javascript :: how to use text onclick to display images in javascript 
Javascript :: convert int to string javascript 
Javascript :: check if user is streaming discord js 
Javascript :: Fill rect in jspdf 
Javascript :: useDebounce 
Javascript :: file upload nest 
Javascript :: can we get the value of form control after disabling it angular 
Javascript :: [JsonConverter(typeof(StringEnumConverter))] on list of enums 
Javascript :: node load testing-check 
Javascript :: custom indicator js tradingview 
Javascript :: how to send message to user in socket.io 
Javascript :: LocomotiveScroll npm 
Javascript :: react counter animation 
Javascript :: duplicate characters in a string javascript 
Javascript :: javaScript getDate() Method 
Javascript :: comparare due array di numeri javascript 
Javascript :: javascript add item by index 
Javascript :: how to disable eval in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =