Welcome to the React JS tutorial where we'll dive into understanding the core concepts of State and Props! These two are fundamental building blocks in a React component. Let's get started! š
In simple terms, State is a data container within a React component, while Props are the data passed from a parent component to a child component.
import React, { Component } from 'react';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
incrementCount = () => {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div>
Count: {this.state.count}
<button onClick={this.incrementCount}>Increment</button>
</div>
);
}
}š” Pro Tip: Use this.setState to update the state.
function MyChildComponent(props) {
return (
<div>
This is a child component with prop: {props.myProp}
</div>
);
}
function App() {
return (
<MyChildComponent myProp="Hello, World!" />
);
}What is the main difference between State and Props in React?
That's it for our first lesson on React JS! By understanding State and Props, you have laid a strong foundation for building React components.
Stay tuned for our next lesson, where we'll dive deeper into working with the state and handle state changes efficiently using React Hooks! šš”š