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
Related
I have one div element and inside it has one button A. and there is button B outside which is hidden by default
so when I click button A, the whole div should hide including button A, and also it should display the hidden button B.
And when I click button B, it should open the div and hide.
How can I achieve this in reactjs.
This is what I have tried, but it is not showing any button on the screen
import React from 'react'
import useState from 'react-dom'
const App = () => {
const [show,setShow]=useState(true)
return(
<>
{show && <div>
Hello
<button>button A</button>
</div>}
<button onClick={()=>setShow(!show)}>button 2</button>
</>
)
Take a look at how this code is setting and using the show boolean state value for Button A and Button B.
const { useState } = React;
const App = () => {
const [show, setShow] = useState(true)
return (
<div>
{show && <div>
Hello
<button onClick={()=>setShow(!show)}>Button A</button>
</div>}
{!show &&
<button onClick={()=>setShow(!show)}>
Button B
</button>
}
</div>
);
}
// Render it
ReactDOM.render(
<App />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>
Note: Make sure you're importing useState from 'react' in your code. I had to write it as const {useState} = React; in order to get the above code snippet to work as expected.
You can write something like below
import React, { useState } from "react";
export default function ToogelButton({ children }) {
// React state to manage visibility
const [showButtonB, setShowButtonB] = useState(false);
// function to toggle the boolean value
function toggleShow() {
setShowButtonB(showButtonB => !showButtonB);
}
if(showButtonB) {
return (
// Button B
<button onClick={toggleShow}>BUTTON B</button>
);
}
return (
// Button A and content
<div className="container">
Test Content
<button onClick={toggleShow}>BUTTON A</button>
</div>
);
}
My ReactJS Modal Window do not work.
Here is code:
import React from 'react';
import './Modal.css';
import Button from 'react-bootstrap/Button';
const Modal = ({ handleClose, show, children }) => {
const showHideClassName = show ? "modal display-block" : "modal display-none";
return (
<div className={showHideClassName}>
<section className="card modal-main">
{children}
<div className="clear"></div>
<Button onClick={handleClose}>
Close
</Button>
</section>
</div>
);
};
export default Modal;
And here is code in my component:
function modalState(state) {
return state;
};
<Modal show={modalState(setModal)} handleClose={modalState(false)}>
{
<div></div>
}
</Modal>
And if I click Add user button then nothing happens:
<Button variant="primary" size="sm" onClick={() => {
setModal = true;
modalState(setModal);
}}>Add User</Button>{' '}
What should I doing wrong?
Try to use useState hook.
Your Modal component.
import { useState } from 'react';
import './Modal.css';
import Button from 'react-bootstrap/Button';
const Modal = ({ show, children }) => {
const [modalState, setModalState] = useState(show);
const showHideClassName = show ? "modal display-block" : "modal display-none";
return (
<div className={showHideClassName}>
<section className="card modal-main">
{children}
<div className="clear"></div>
<Button onClick={() => setModalState(false)}>
Close
</Button>
</section>
</div>
);
};
export default Modal;
And your component where calls Modal component.
import { useState } from 'react';
import Modal from './path/Modal';
const CustomComponent = () => {
const [modalState, setModalState] = useState(false);
return (
<>
<Modal show={modalState}>
{
<div></div>
}
</Modal>
<Button
variant="primary" size="sm"
onClick={() => setModalState(true)}
>
Add User
</Button>
</>
);
}
export default CustomComponent;
But please note that this would be just a temporary solution based on your current codes. If you want a better and permanent solution, I'd like to recommend to implement the global state management thru your whole codebase using Redux or Recoil.
In the onclick event useState set is not done to set the value of the Modal
setModal({modal:true})
And
<Modal show={modal} handleClose={modalState(false)}>
{
<div></div>
}
</Modal>
I think if you do this it will work.
const [isVisibleModal, setVisibleModal] = useState(false); // <- add
function closeModal() {
setVisibleModal(!isVisibleModal);
};
<Modal show={isVisibleModal} handleClose={closeModal}>
{
<div></div>
}
</Modal>
<Button
variant="primary"
size="sm"
onClick={() => setVisibleModal(true)}
>
Add User
</Button>
Hope this helps you!
You should use setVisibleModal function same page like this code
import React, { useState } from 'react';
const YourComponent = () => {
const [isVisibleModal, setVisibleModal] = useState(false); // <- add
function closeModal() {
setVisibleModal(!isVisibleModal);
};
return (
<>
<Modal show={isVisibleModal} handleClose={closeModal}>
{
<div></div>
}
</Modal>
<Button
variant="primary"
size="sm"
onClick={() => setVisibleModal(true)}
>
Add User
</Button>
</>
)
}
export default YourComponent;
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 };
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;
}
Hi I'm new to React and building few things in React and this may seem a very generic question.
I want to show a table on click of button. Below is my code.
import React from 'react';
import { Link }
import Button from 'react-bootstrap/lib/Button';
import Panel from 'react-bootstrap/lib/Panel';
import Grid from 'react-bootstrap/lib/Grid';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
import ButtonGroup from 'react-bootstrap/lib/ButtonGroup';
import FormGroup from 'react-bootstrap/lib/FormGroup';
this.state = {
showSubmit: false,
};
submitForm = () => {
window.alert('test');
}
toggleSubmitForm = () => {
this.setState({
showSubmit: !this.state.showSubmit
});
window.alert('test2');
}
export default (props) => {
return (
<AppLayout title="Table Con" activeModules={props.activeModules}>
<Protected>
<div className="container-fluid">
<h4>
Welcome to the page
!
</h4>
</div>
<Button
className="btn btn-secondary"
bsSize="small"
onClick={this.toggleSubmitForm}
>
Show Table
</Button>
{this.state.showSubmit && (
<div className="container-fluid well" id="submitT">
<form onSubmit={this.submitForm}>
<Grid>
<Row>
<Col xs={12}>
<div>
<h3>HERE</h3>
</div>s
<br />
<br />
</Col>
</Row>
</Grid>
<Button type="submit" bsStyle="success" bsSize="large">
Submit
</Button>
</form>
</div>
)}
</Protected>
</AppLayout>
);
};
But when onClick is called, nothing is happening.
I'm not sure where I'm failing.
Also, if i want to call a mongo collection and render the table after I click on Show Table button. What are the changes to be made ?
As #noitse pointed out, you are mixing statefull and stateless component features.
However, React added a new alternative if you want to keep your component as a function, Hooks. Here's what you code will look like as a hook :
import { useState } from 'react'
export default props => {
const[showSubmit, setShowSubmit] = useState(false)
return (
<AppLayout title="Table Con" activeModules={props.activeModules}>
<Protected>
<div className="container-fluid">
<h4>Welcome to the page !</h4>
</div>
<Button className="btn btn-secondary" bsSize="small" onClick={setShowSubmit(true)}>
Show Table
</Button>
{showSubmit && /* Your table*/}
</Protected>
</AppLayout>
);
};
You are combining functional and class component features.
Functional components do not have access to the state unless you are using useState feature (16.3 update). Any "this." is basically undefined in your code.
Rewrite your component like this:
import React, {Component} from 'react' // or PureComponent
// ...other imports
class YourComponent extends Component {
state = {
showSubmit: false
}
submitForm = () => { /* what ever */}
toggleSubmitForm = () => {
this.setState({showSubmit: !this.state.showSubmit})
}
render(){
return(
... your render code
)
}
}
export default YourComponent