GithubHelp home page GithubHelp logo

kylenrich24 / react-helper3_classcomponenents Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 567 KB

πŸŒ€ React-Helper3_Class Componenents

Home Page: https://kylenrich24.github.io/React-Helper3_ClassComponenents/

HTML 31.49% CSS 34.52% JavaScript 33.98%

react-helper3_classcomponenents's Introduction

πŸŒ€ React-Helper3_Class Componenents πŸŒ€

πŸŒ€ Class-Based Components



To generate a functional component in VSCode: rcc

Β πŸŒ€Β  Functional Component - good for simple content without logic behind it
Β πŸŒ€Β  Class-Based Component - good for everything else

  • Easier code organization
  • State - Β Β  easier to handle user input
  • Lifecycle Events - Β Β  easier to do things when app first starts


import React, {Components} from 'react'

class SeasonDisplay extends Components {
 render() {                              // the only requirement of a class function is to have a render method
  return (
   <div></div>
   )
 }
}

export default SeasonDisplay


πŸŒ€ State


Β πŸŒ€Β  JS object that contains data relevant to a component
Β πŸŒ€Β  Updating state on a component causes the component to rerender
Β πŸŒ€Β  State must be initialized when a component is created
Β πŸŒ€Β  State can only be updated with setState()


using state

class SeasonDisplay extends Component {
 constructor(props) {                // we are overwriting our parents constructor
  super(props)                       // retaining our parents constructor and just adding to it
  this.state = {lat: null }          // our state; initializing it to null because we're expecting a number 
  
  getLocation(
   position => this.setState ({lat: position.latitude})    // changing our state
  )
 }
 
 render() {                     // required for a class component
  return JSX
 }
}


πŸŒ€ Lifecycle Methods


These are methods in our Class-Based Components. React automatically call them at certain points during a components lifecycle. A component's lifecycle goes like this: gets created, shows up in DOM, rerender and in theory gets removed in the DOM altogeher

For instance after render(), componentDidMount() gets called one time after our component gets rendered in our screen. We can put some codes here for our component setup.


Β πŸŒ€Β Β  constructor - good place to do one-time setup
Β πŸŒ€Β Β  render - only for returning JSX
Β πŸŒ€Β Β  componentDidMount - good place to do data loading
Β πŸŒ€Β Β  componentDidUpdate - good place to do more data-loading when state/props change
Β πŸŒ€Β Β  componentWillUnmount - good place to do cleanup




class App extends Component{
 
 state={ lat: null}                                        // this is an easier alternative for initializing state; Babel
 
 componentDidMount(){                                      // we take care of data loading here
  getLocation(
   position => this.setState ({lat: position.latitude})    // changing our state
  )
 }
 
 render() {
  return <div>{this.state.lat}</div>
 } 
}

export default App

πŸŒ€Β Ref System


Getting a DOM element in React

 constructor(props) {                       // we create the ref in the constructor
    super(props);

    this.imageRef = React.createRef();
  }
 
 render() {
  return (
   <img ref={this.imageRef} />             // we pass the createf ref to <img />; we get the <img />
  )                                        // use this.imageRef
 }

react-helper3_classcomponenents's People

Contributors

kylenrich24 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.