Giter Site home page Giter Site logo

react-two's Introduction

React Two

In this lecture we will go more in depth about data and how it flows in the React ecosystem.

Lecture Slides

https://slides.com/matias_perez/react-two#/

Student Learning Objectives

  • Student can pass hard coded data via props
  • Student can pass data from state via props
  • Student can pass functions via props
  • Student can bind function
  • Student understands they need to bind any function passed as a prop
  • Student can access this.props in a child to get data from the parent
  • Student can invoke a function from a parent that was passed via props
  • Student can pass data back to a parent via a props function

Data Flow

React will handle its data using unidirectional data flow. This means that data is passed down from the top of the application to the bottom. We can determine what top and bottom are using our component architecture design.

We can use events to send data back up the component tree.

DataFlow

Values from a components state or methods can be passed to a child component through props

Props

Using props allow us to pass data from a parent component to a child component. We do this rendering a child component inside of our JSX then setting an attribute on the rendered component with the data that we want to pass as a value for the attribute.

Passing Props From Parent Component

import React from 'react';

// Import the child component
import ChildComponent from './ChildComponent';

class ParentComponent extends React.Component {
    constructor(){
        super();

        this.state = {
            name: 'Tayte'
        }
    }

    render(){
        return (
            <div>
                // Render the child component inside of the parents JSX
                // Apply props to it to pass the data
                <ChildComponent myName={this.state.name}/>
            </div>
        )
    }
}

export default ParentComponent

Receiving Props Functional Component

Receiving props as a functional component is different then how we receive them as a class component. In the functional component, we will need to set a parameter to allow us to receieve the props object as an argument. We can then access the data from that object.

Let's create the child component from the above example as a functional component:

import React from 'react';

const ChildComponent = (props) => {
    // notice how we have a param set to receive the props object

    return (
        // We can then use this object to grab the prop from it
        <h1>My name is: {props.myName}</h1>
    )
};

export default ChildComponent;

Receiving Props Class Component

Receiving props as a class component is not the same as a functional component. We no longer need to set a parameter in any way to receive the props object. This object is actually built into the class component so we will access it through the class itself using the this keyword.

Let's create that same child componet but in class form:

import React from 'react';

class ChildComponent extends React.Component {
    render(){
        return (
            // notice how we use the `this` keyword
            <h1>My name is: {this.props.myName}
        )
    }
}

export default ChildComponent;

Passing Methods

We can also pass methods from one component to another component so that the functionality inside of the nested component can update the state of where that method was created.

When we pass methods we need to make sure that we bind them to the component they were created in.

In the constructor is where you want to bind your methods.

import React from 'react';

class ParentComponent extends React.Component {
    constructor(){
        super();

        this.state = {
            name: 'Tayte'
        }

        // bind methods here
        this.changeName = this.changeName.bind(this);
    }

    changeName(){
        // use setState to update state values
        this.setState({
            name: 'Tayte V2'
        })
    }

    render(){
        return (
            <div>
                // we are now passing multiple props one from state and the other the method to update the state
                <ChildComponent myName={this.state.name} changeName={this.changeName}/>
            </div>
        )
    }
}

class ChildComponent extends React.Component {
    render(){
        return (
            <div>
                <h1>My name is: {this.props.myName}</h1>
                // use the method on the event of the button
                <button onClick={this.props.changeName}>Update Name</button>
            </div>
        )
    }
}

export default ChildComponent;

Additional Resources

General

Articles

Videos

react-two's People

Contributors

matias-perezferrero avatar mattcbodily avatar mutantrobbyk avatar taytestokes avatar

Watchers

 avatar

Forkers

wlh-16

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.