Home » React JS

Introduction to React JS Properties (props)

React JS Properties (props): In this article, we are going study React JS properties commonly called props.
Submitted by Godwill Tetah, on November 21, 2019

React JS properties or props give more functionality to a react component.

Like most developers always say, React JS props are like HTML attributes and can also be seen as JavaScript function parameters.

Props can be added to a component the same way an attribute is added to an HTML element.

The component then receives the prop as an object. Therefore, accessing the props will similar to accessing items of a JavaScript object.

Syntax:

    <App type="android" />

Where, "App" is the name of the component.

Example:

Quickly open the index.js file your React JS application and adjust the ReactDOM.render() line.

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import './index.css'

ReactDOM.render(< App kind= "android" />, document.getElementById('root'))

Let's create a file now for App component called App.js.

From our App.js file, we are going to print out the property or props of the component using the same syntax to access elements of a JavaScript object.

import React from "react"

class App extends React.Component {
	render (){
		return (
			<h1> This app is  {this.props.kind} </h1> 
		)
	}
}
export default App

Output

React Js Properties

From the example above, we added property called "kind" to the App component. The property is then accessed from the props object using the syntax this.props.kind.

Props are read-only, meaning you can’t change it in a different line of code.

From ES6, the constructor function is used to create props in React JS components using the following syntax,

class Car extends React.Component {
	constructor(props) {
		super(props)
	}
}
render() {
	return (
		<div>
		....
		</div>
	)
}

Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.