Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

React - state and props in component

0 votes
7 views
added Mar 12 in React by LC Marshal Captain (20,510 points)
edited Mar 12 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;

 

Please log in or register to response this reference.

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