I am trying to appear text from main App.js to component via props. How can take Todos text from App.js to Modal Component in Modal Heading Text. Such as
Are you sure? "and the name of Todo"
App.js
import Todo from "./components/Todo";
function App() {
return (
<div>
<h1>My Todos</h1>
<Todo text="Learn React" />
<Todo text="Master React" />
<Todo text="Explore the full React course" />
</div>
);
}
export default App;
Modal Component Code
function Modal(props){
function cancelhandler(){
props.onCancel();
}
function confirmlhandler(){
props.onConfirm();
}
return (
<div className="modal">
<p>Are you sure?</p>
<button className="btn btn--alt" onClick={cancelhandler}>Cancel</button>
<button className="btn" onClick={confirmlhandler}>Confirm</button>
</div>
);
}
export default Modal;
Todo Component
import Modal from './Modal';
import Backdrop from './Backdrop';
function Todo(props){
return (
<div className="card">
<h2>{props.text}</h2>
<div className="actions">
<button className="btn">Delete</button>
</div>
</div>
);
}
export default Todo;
You can pass text from Todo to Modal as:
<Modal text={props.text} />
CODESANDBOX LINK
and show it in Modal as:
<p>Are you sure you want to delete {props.text}?</p>
There are a few ways to pass this value.
By props:
In Todo Component when declaring Modal Component you can pass variable
components/Todo.jsx
<div className="card">
<h2>{props.text}</h2>
modalStatus ? <Modal headline={props.text} /> : null; // I have used state here to determine when modal shall be visible
<div className="actions">
<button className="btn">Delete</button>
</div>
</div>
components/Modal.jsx
<div className="modal">
<p>Are you sure? {props.headline}</p>
<button className="btn btn--alt" onClick={cancelhandler}>Cancel</button>
<button className="btn" onClick={confirmlhandler}>Confirm</button>
</div>
If you have a few components when you want to pass variables you should use useContext instead of props drilling.
components/Todo.jsx
import Modal from "./Modal";
import modalContext from "../contexts/modalContext";
import { useState } from "react";
function Todo(props) {
const [modalOpen, setModalOpen] = useState(false);
return (
<div className="card">
<h2>{props.text}</h2>
<modalContext.Provider value={props.text}>
{modalOpen ? <Modal /> : null}
</modalContext.Provider>
<div className="actions">
<button className="btn">Delete</button>
</div>
</div>
);
}
export default Todo;
contexts/modalContext.jsx
import { createContext } from "react";
const modalContext = createContext(null);
export { modalContext };
Related
App.js
import NewsSec from "./News/NewsSec";
import ScoreSec from "./ScoreSec/ScoreSec";
import Menu from "./Sidebar/Menu";
import "./styles.css";
import { GiHamburgerMenu } from "react-icons/gi";
import React, { useState } from "react";
export default function App() {
const [showMediaIcons, setShowMediaIcons] = useState(false);
return (
<div className="App">
<div className="head">
<div className="navicon">
<a
href="/"
onClick={() => {
setShowMediaIcons(!showMediaIcons);
}}
>
<GiHamburgerMenu />{" "}
</a>
</div>
<div className="logo">Project</div>
<div className="weather">weather section</div>
</div>
<div className="main">
<div className="nav-section">
<Menu classes={showMediaIcons ? "mobile-view navbar" : "navbar"} />
</div>
<div className="news-section">
<NewsSec />
</div>
<div className="score-section">
<ScoreSec />
</div>
</div>
</div>
);
Menu.js
import React from "react";
import "./Navbar.css";
const Menu = (props) => {
return (
<>
<div className={props.classes}>
<ul>
<li>Home</li>
<li>News</li>
<li>Sports</li>
<li>Weather</li>
<li>About</li>
</ul>
</div>
</>
);
};
export default Menu;
i was trying to make a responsive navigation bar. the navigation bar is actually a sidebar. i used the props to pass the 'className' from App.js to Menu.js because i called the function in App.js
For testing, I tried changing the nav colour to Red. But on clicking Hamburger icon, the colour changes to Red and changed back to normal automatically. Please help folks
It seems that a might be reloading the page with href="/", resetting the state showMediaIcons to its initial value of false.
If the purpose of GiHamburgerMenu is to toggle display for Menu, it might not need to be wrapped in a, instead try add the onClick on the icon or on the wrapping div:
<div className="navicon">
<GiHamburgerMenu
onClick={() => {
setShowMediaIcons((prev) => !prev);
}}
/>
</div>
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
I'm new to react and I would like to build a dynamic button, I wrote this code but I don't know How I can pass text, style, and size to this button.
import "./button.css";
import React from "react";
const Button = ({
type,
onClick,
buttonStyle,
buttonSize,
}) => {
return (
<div>
<div>
<button
type={type}
onClick={onClick}
buttonStyle={buttonStyle}
buttonSize={buttonSize}
></button>
</div>
</div>
);
};
export default Button;
In React, html elements does not have buttonStyle and buttonSize attribute. The onClick attribute and the type attribute are valid.
You also need to pass in a props parameter in your Button Const Button(props) and add a style attribute which will contain your style declarations in key:value pair.
Also in order to be dynamic, your button should have a label. you can achieve this by adding props.buttonLabel
Your code should be similar to this code below:
import "./button.css";
import React from "react";
const Button = (props) => {
return(
<div>
<div>
<button
type={props.type}
onClick={props.onClick}
style={props.buttonStyle}
>
{props.buttonLabel}
</button>
</div>
</div>
);
};
export default Button;
I think I found the solution...
import "./button.css";
import React from "react";
const Button = ({ parentClassName, buttonLabel, type, onClick }) => {
return (
<div>
<div>
<button className={parentClassName} type={type} onClick={onClick}>
{buttonLabel}
</button>
</div>
</div>
);
};
export default Button;
ReactDOM.render(
<React.StrictMode>
<Button
type="button"
onClick={() => console.log("you clicked me")}
buttonLabel="Bye Now"
parentClassName="btn btn-secondary"
/>
</React.StrictMode>,
document.getElementById("root")
);
Blockquote
By default I am trying to show button, Now I am trying to hide button when I click the buton in react using functional components.
This is my code
This is App.js
import React, { useState } from 'react';
import Parent from './Parent/Parent';
import './App.css';
function App() {
return (
<div className="App">
<Parent></Parent>
</div>
);
}
export default App;
This is Parent.js
import React, { useState } from 'react';
import './Parent.css';
const Parent = () => {
const [show, hide] = useState(true)
const hideButton = () => {
hide(false)
}
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='one'>
<button show ={show} onClick={hideButton} className='btn btn-primary'>Click here</button>
</div>
</div>
</div>
</div>
)
}
export default Parent
You need to do ternary condition to show and hide value:
{show && <button onClick={hideButton} className='btn btn-primary'>Click here</button>}
Full code:
import React, { useState } from "react";
import "./styles.css";
const Parent = () => {
const [show, hide] = useState(true);
const hideButton = () => {
hide(false);
};
return (
<div className="container">
<div className="row">
<div className="col-12">
<div className="one">
{show && (
<button onClick={hideButton} className="btn btn-primary">
Click here
</button>
)}
</div>
</div>
</div>
</div>
);
};
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<Parent />
</div>
);
}
Here is the demo: https://codesandbox.io/s/romantic-newton-1wvl1?file=/src/App.js:0-678
I am working on React project, In that I have App.js component, in that component I have button Now please tell me how to change button background color and button text color by using react hooks
This is App.js
import React, { useState } from 'react';
import './App.css';
function App() {
return (
<div className="App">
<button className='btn btn-primary'>Click here</button>
</div>
);
}
export default App;
try this
function App() {
const [color,setColor]=useState('red');
const [textColor,setTextColor]=useState('white');
return (
<div className="App">
<button style={{background:color,color:textColor}} className='btn btn-primary' onClick={()=>{setColor("black");setTextColor('red')}}>Click here</button>
</div>
);
}
export default App;
OR
check link : https://stackblitz.com/edit/react-x7mevv
App.js
function App() {
const [toggle,setToggle] = React.useState(false);
const toggleIt =()=>{
setToggle(!toggle)
}
return (
<div className="App">
<button onClick={toggleIt}>{toggle?'Hide':'Show'}</button>
<div className={toggle&&'body-color'}>
Body
</div>
</div>
);
}
class in App.css
.body-color{
background: coral;
height:100vh;
}