ReactJS - 3 ways to Import components in ReactJS
Introduction In this post, we will discuss 3 different ways to import a…
May 08, 2021
I have a parent component which loads some list categories. Based on the selection of this category, I want to show some data in a table. And, I have a table component which displays that data.
So, based on selectio, data on child component must change.
The solution is to maintain state in parent component, and pass the props to child component. And the child component, will read data from props.
import apiClient from '../../components/api/api_client'
import SimpleLayout from '../../components/layout/simple'
import React, { Component } from 'react'
import { Form } from 'react-bootstrap';
import EmailTable from '../../components/emailView/emailTable'
export default class Users extends Component {
constructor(props) {
super(props);
this.state = {
emails: []
};
}
handleSelection = async (event) => {
this.setState({
emails: await apiClient.getEmailsForList(event.target.value)
});
}
render() {
const emaillist = this.props.emaillist;
return (
<SimpleLayout>
<div className="row">
<Form>
<Form.Group controlId="exampleForm.SelectCustomSizeLg">
<Form.Label>Email Lists</Form.Label>
<Form.Control as="select" custom
onChange={this.handleSelection}>
{emaillist && emaillist.map((each, index) => {
return(
<option key={index} value={each.name}>{each.name}</option>
)
})}
</Form.Control>
</Form.Group>
<Form.Group>
<EmailTable emails={this.state.emails} />
</Form.Group>
</Form>
</div>
</SimpleLayout>
)
}
}
Now the child component will just render data from its props.
import React, { Component } from 'react'
import { Table } from 'react-bootstrap';
export default class EmailTable extends Component {
render() {
return (
<Table striped bordered hover variant="dark">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{this.props.emails.map((each, index) => {
return(
<tr key={index}>
<td>#</td>
<td>{each.email}</td>
<td>{each.email}</td>
<td>{each.status}</td>
</tr>
)
})}
</tbody>
</Table>
)
}
}
Hope it helps.
Introduction In this post, we will discuss 3 different ways to import a…
Introduction Lets take a look at how forms are being handled in ReactJS. We will…
Problem Statement I developed a simple ReactJS application where I have used…
Introduction While this topic may applicable to all mysql/mariadb users who…
Code that I have is: It was: I changed it to: So, I needed to change button type…
ReactJS setState is Asynchronous setState() method in ReactJS class components…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…