How to change styles for components while we reusing in react? - reactjs

I am working on react project, In that I have a parent component that is App.js. for that that App.js, Child.js is child component. In that Child.js I put one icon and apply styles.
Now I want to reuse that Child.js component with a different background color, different icon, different border color.
How to achieve this is in react
This is App.js
import React from 'react';
import './App.css';
import Child from './Child/Child'
function App() {
return (
<div className="App">
<Child></Child>
<Child></Child>
</div>
);
}
export default App;
This is Child.js
import React from 'react';
import './Child.css';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faCoffee } from '#fortawesome/free-solid-svg-icons';
function Child() {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div class="exp">
<FontAwesomeIcon className='coffee' icon={faCoffee} />
</div>
</div>
</div>
</div>
)
}
export default Child
This is Child.css
.exp{
width:80px;
height:80px;
background-color:red;
border-radius:100%;
line-height:80px;
text-align:center;
vertical-align:middle;
display:inline-block;
border: solid 3px blue;
}
.coffee {
color: green;
}

You can create multiple classes in your css file and then pass in the prop of the class name and icon you want to use in the component.
<Child iconClass="coffee" icon={faCoffee}/>
function Child(props) {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div class="exp">
<FontAwesomeIcon className={props.iconClass} icon={props.icon} />
</div>
</div>
</div>
</div>
)
}

You can add an icon props to render a JSX element in your Child component.
function Child({ icon }) {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div class="exp">
{icon}
</div>
</div>
</div>
</div>
)
}
You can then write your Children component like this in your Parent component.
<Child icon={(<FontAwesomeIcon className='coffee' icon={faCoffee} />)} />

Make the child to accept props and pass props when you render the component.
function Child({ icon, className}) {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div class="exp">
<FontAwesomeIcon className={className} icon={icon} />
</div>
</div>
</div>
</div>
)
}
export default Child
Then use it like
<Child icon="faCoffee" className="style" />
<Child icon="faBars" className="style" />

Related

React Carousel and onClick events

I'm making a small imdb clone in reactJS and I just managed to add a carousel to the component that renders out movies. The problem is that the clickevent for each movie doesn't work anymore. What's the simplest way to solve this without rewriting the entire component?
I could add a onclick event to the carousel but then i have to return all the neccessary data from the movie component and later send it to moviePage which seems redundant
Home.js
import { React } from "react";
import SearchBox from "./components/SearchBox";
import Movie from "./components/Movie";
import { Carousel } from "react-responsive-carousel";
function Home(props) {
return (
<div className="App">
<div className="row d-flex align-items-center mt-4 mb-4">
<SearchBox
searchValue={props.searchValue}
setSearchValue={props.setSearchValue}
/>
</div>
<h1>Movies</h1>
<div className="block">
<Carousel onClickItem={console.log("you pressed")}>
{props.data.map((movie, index) => {
return (
<Movie
key={movie.id}
movie={movie}
placeMovie={props.placeMovie}
index={index}
/>
);
})}
</Carousel>
</div>
<h1>Recently viewed</h1>
<div className="inline-view">
{props.latestView.map((n) => {
return (
<img
key={n.id}
src={"https://image.tmdb.org/t/p/w500" + n.backdrop_path}
alt={n.poster_path}
/>
);
})}
</div>
</div>
);
}
export default Home;
Movie.js
import React from "react";
import { NavLink } from "react-router-dom";
export default function Movie(props) {
const handleClick = () => {
props.placeMovie(props.index);
};
return (
<div className="image-container d-flex justify-content-start m-3">
<div>
<NavLink onClick={handleClick} to={`/MoviePage`}>
<img
className="image-list"
src={"https://image.tmdb.org/t/p/w500" + props.movie.backdrop_path}
alt={props.movie.path}
/>
</NavLink>
<h4 class="font-weight-bold">{props.movie.title}</h4>
<h5 class="font-weight-light">Score:{props.movie.vote_average}</h5>
</div>
</div>
);
}
Heres the moviepage that displays more info when you click on a movie item
import React from "react";
function MoviePage(props) {
return (
<div>
<div className="card-single">
<div>
<img
className="image-single"
src={"https://image.tmdb.org/t/p/w500" + props.movie.backdrop_path}
alt={props.movie.path}
/>
<div class="card-title">
<h1 class="font-weight-bold">{props.movie.title}</h1>
</div>
<h3 class="card-text">{props.movie.overview}</h3>
<h5 class="font-weight-light">
Language: {props.movie.original_language}
</h5>
<h5 class="font-weight-light">
Release date: {props.movie.release_date}
</h5>
</div>
</div>
</div>
);
}
export default MoviePage;

My background img is not working in react app

my background image is not working can any buddy check it?
import React from 'react'
import "./Home.css"
// backgroundimage
import bgImg from "./../assests/img/bg-sec1.jpg"
function Home() {
return (
<div>
<div styles={{ backgroundImage:`url(${bgImg})` }}>
<h1>This is red car</h1>
</div>
</div>
)
}
function Home() {
return (
<div>
<div style={{ backgroundImage:url(${bgImg}) }}>
<h1>This is a red car</h1>
</div>
</div>
)
}
At least you must fix typo styles->style.

Passing Data from Parent to Child In React

I am testing if I can pass data from the parent App.js to a child component named Contact. I want to pass data when clicking on a button.
Here is the App.js file:
import React, { useState } from 'react';
import { Button } from 'semantic-ui-react';
import './App.css';
import Header from './Components/Header';
import Tabs from './Components/Tabs';
import Footer from './Components/Footer';
import SimpleMap from './Components/SimpleMap';
import NewPatient from './Components/NewPatient';
import Contact from './Components/Contact';
function App() {
const [data,setData] = useState(false);
const parentToChild = () => {
setData("This is information from parent to child.");
}
return (
<div className="App">
<Header />
<Contact parentToChild={data} />
<Tabs>
<div label="Home" class="home-page">
<img src="https://news.usc.edu/files/2020/06/covid_vaccine_stock.jpg"></img>
</div>
<div label="New Patient">
<NewPatient />
</div>
<div label="Locations">
<SimpleMap />
</div>
<div label="Appointments">
</div>
</Tabs>
<Footer />
</div>
);
}
export default App;
Here is the child component I want the data to be displayed from the parent data when I click on the button:
import React from 'react';
import './footer.css'
function Footer({parentToChild})
{
return (
<div className="Footer">
<div className="container">
<div className="row">
<div className="col">
<h4 className="block ">About Us</h4>
</div>
<div className="col">
<h4 onClick={() => parentToChild(true)} className="block" >Contact</h4>
</div>
<div className="col">
<h4><a className="block" href={"https://www.cdc.gov/coronavirus/2019-ncov/faq.html"}>COVID-19 FAQ</a></h4>
</div>
<div className="col">
<h4 className="block"><a className="block" href={"https://www.cdc.gov/coronavirus/2019-ncov/if-you-are-sick/quarantine.html"}>CDC Guidelines</a></h4>
</div>
</div>
</div>
</div>
);
}
export default Footer;
I am testing this functionality because I want to eventually display a popup form when I click on a button. I want to use the data from the parent to trigger a popup in a child component. The following code says that parentToChild is not a function. Why do I get this error?
You need to pass your parentToChild function to your Footer Component:
<Footer parentToChild={parentToChild}/>
And if you want to show data from your child, you will need to declare it in your params:
const parentToChild = (input) => {
console.log('Info from child' ,input);
setData("This is information from parent to child.");
}
You must pass that information via props to the Footer component,
<Footer parentToChild={parentToChild} />
This will trigger the callback.
you can use state.
const [state, setState] = useState('')
App.js
const buttonThatYouWantTobeClicked = (dataShouldGotoComponent) => {
setState(dataShouldGotoComponent)
}
<Footer data={state}/>
then in footer.js you can check if value of props is not undefiend or null, so do something

export partial component in React functional component

In the below code I don't want to export Carousel and I just want to import everything except Carousel in other component.
import React from "react";
function Navbar() {
};
return (
<div className="navlabar">
<div className="brand_name_logo">
<Link to="/">
<img src={brand_name} alt="brand_logo" />
</Link>
</div>
<div className="dropdown">
<div>
<div id="explore__dropdown" className=" hide__ani hide__content">
<div className="explore__all__project">
<img src={exploreallproject}></img>
</div>
<div className="explore__top__10__find">
<img src={top10find}></img>
</div>
</div>
<Carousel climber1="CLMBR" />
</div>
);
}
export default Navbar;

How to pass an Array from Child component to Parent component using hooks in react

I am working on a React project I am trying to pass a data from Child component to Parent component, But I don't know how to pass data from Child component to Parent component.
This is App.js
import React from 'react';
import './App.css';
import Parent from './Parent/Parent';
function App() {
return (
<div className="container">
<div className='row'>
<div className='col-12'>
<Parent></Parent>
</div>
</div>
</div>
);
}
export default App;
This is Parent.js
import React, { useState } from 'react';
import './Parent.css';
import Child from '../Child/Child';
const Parent = () => {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='one'>
<Child></Child>
</div>
</div>
</div>
</div>
)
}
export default Parent
This is Child.js
import React from 'react';
import './Child.css';
const Child = () => {
const students = ['Jasmine', 'Stella']
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
</div>
</div>
</div>
)
}
export default Child
You can pass the data from Child to Parent by calling a function passed from Parent with the data you want to send as a parameter - like an event handler callback.
Let's say if you want to send students of your Child component to Parent when you click on an element:
const Parent = () => {
const onUpdate = (data) => {
// use the data
};
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='one'>
<Child update={onUpdate}></Child>
</div>
</div>
</div>
</div>
)
}
const Child = ({ update }) => {
const students = ['Jasmine', 'Stella']
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<button click={() => update(students)} />
</div>
</div>
</div>
)
}
But if you have to pass data between components nested deeply, you should consider to use Redux or Context.
To do this, You have two easiest ways,
Use React context
Use React prop callback
I suggest you use the first way and here is an example of implementing the redux context feature into your app based on your question's codes.
Demo

Resources