React - state and props in component

0 votes
199 views
added Mar 12, 2019 in React by LC Marshal Captain (25,790 points)
edited Mar 12, 2019 by LC Marshal
import React from 'react'

class StateAndPropsComponent extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            //state declared here
            thisheader: 'StateAndPropsComponent here',
            thiscontent: 'StateAndPropsComponent content here'
        }
    }
    render () {
        return (
            <div>
                //populate class (Sub)component using data from state
                <Header headerProp = {this.state.thisheader}/>
                <Content contentProp = {this.state.thiscontent}/>
            </div>
            
        )
    }
}

class Header extends React.Component {
    render () {
        return (
            <div>
                //JSX string of headerProp called here, using data from state
                <h3>{this.props.headerProp}</h3>
            </div>

        )
    }
}

class Content extends React.Component {
    render () {
        return (
            <div>
                //JSX string of contentProp called here, using data from state
                <p>{this.props.contentProp}</p>
            </div>

        )
    }
}

export default StateAndPropsComponent;

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...