In ReactJS, a component is a piece of reusable UI that can be rendered to the screen. Components are the building blocks of a ReactJS application, and they allow you to split your application into smaller, more manageable pieces. Components can receive data as inputs (props), and they can render different outputs based on the props they receive.
With PrimeReact, you can create custom components by extending the Component class in ReactJS. PrimeReact also provides a rich set of UI components that you can use in your application, such as buttons, data tables, and form inputs.
To create a component in PrimeReact, you need to define a class that extends the Component class in ReactJS. The component class must implement the render method, which returns the component's UI.
Here's an example of a simple component in PrimeReact:
import React, { Component } from 'react';
import { Button } from 'primereact/button';
class MyComponent extends Component {
render() {
return (
<Button label="Click Me" />
);
}
}
export default MyComponent;
To render the component, you can use the component in another component or in the root component of your application:
import React, { Component } from 'react';
import MyComponent from './MyComponent';
class App extends Component {
render() {
return (
<MyComponent />
);
}
}
export default App;
Props are inputs that can be passed to a component to change its behavior. In PrimeReact, you can pass props to a component by including them as attributes in the component's JSX tag.
Here's an example of passing a prop to a component:
import React, { Component } from 'react';
import { Button } from 'primereact/button';
class MyComponent extends Component {
render() {
return (
<Button label={this.props.label} />
);
}
}
export default MyComponent;
And here's how you can pass the prop to the component:
import React, { Component } from 'react';
import MyComponent from './MyComponent';
class App extends Component {
render() {
return (
<MyComponent label="Click Me" />
);
}
}
export default App;
In this example, the label prop is passed to the MyComponent component, and the component's render method uses the prop to set the label of the Button component.