react trying to set multiple values with an onclick button - reactjs

When the user clicks a payment method card it should set the values of the usestates to the respective values.
How do I assign multiple values in the onClick button function? Thanks.
<div className="col-md-auto" key={p.id}>
<div className="card mb-4 shadow-sm">
<div className="card-body">
<p className="card-text"><b>{p.card_number}</b></p>
<p className="card-text"><b>{p.expiry_date}</b></p>
<p className="card-text"><b>{p.cardholder_name}</></p>
<p className="card-text"><b>{p.sort_code}</b></p>
<p className="card-text"><b>{p.cvv_number}</b></p>
<div className="btn-group mr-2">
<button
onClick={e => setExpiry_date(p.card_number),setExpiry_Date(p.expiry_date)}>
Click me!
</button>
</div>
</div>
</div>
</div>

I think having a handler might make it a bit more readable and you might be getting the errors because of the syntax and some missing commas.
I don't know if it's a mistake, but you are doing setExpiryDate twice in your handler for both card number and expiry date. I think there should be two different values. Will you mind sharing your whole component code for more clarity?
Also, try to use camelCase for all handers, some linters might not allow snake cases and React also uses camelCase itself.
Can you see if the following works for you.
const Component = () => {
const handleButtonClick = () => {
setCardNumber(cardNumber);
setExpiryDate(expiryDate);
}
return (
<button onClick={handleButtonClick}>
Click me
</button>
);
}

Related

React Responsive Carousel returns all items in array on single carousel page

I am trying to utilize React Responsive Carousel. Currently, with my implementation, this code returns all objects in the array on the 1st page of the carousel (rending the point of a carousel useless). I'd like to have it so that each page of the carousel only shows one object in the array and you would need to utilize the "next" or "arrow" button via the carousel to view the next object in the array.
I tried to change profiles.map to profiles.forEach though this doesn't return any object at all.
I'm currently in a coding bootcamp, so forgive me if the code is bad altogether.
const { loading, data } = useQuery(QUERY_PROFILES);
const profiles = data?.profiles || [];
return (
<Carousel>
<div className="flex-row justify-space-between my-4">
{profiles &&
profiles.map((profile) => (
<div className="card col-12 col-xl-6" key={profile._id}>
<div className="card-body">
<h3 className="card-title">
{profile.firstName} {profile.lastName} <br />
<span className="mechLoc">
Location: {profile.location}
</span>
</h3>
<Link
className="seeProfileBtn btn btn-primary"
to={`/profiles/${profile._id}`}
>
See mechanic profile
</Link>
</div>
</div>
))}
</div>
</Carousel>
);
}

How can I change React state from nested dynamically created components?

I am pulling project info from database and then based on how many projects I have, I push to an array and render. The problem is for some reason the "dropdownOptions" will not toggle between showing and hiding.
I basically just want to be able to click the dropdown arrow and have it show options like "delete" or "settings". Also not sure if I need to have a separate id for each project component created.
Code Snippets:
// Using hook to set display of dropdown to false
const [dropdownOptions, setDropdownOptions] = useState(false);
// This piece automatically renders clickable projects from database
const pList = [];
for (var i = 0; i < project_list.length; i += 1) {
var projectName = project_list[i]
pList.push (
<div className="projects" key={i}>
<div className="projectName">Name: <div className="projectText">{projectName}</div></div>
<div className="projectDate"> Date Created: <div className="projectText">{date}</div></div>
<div className="projectLabels">LabelsLeft:</div>
<div className="drop">
<div className="dropArrow" onClick{setDropdownOptions(!dropdownOptions)}>
{dropdownOptions ? <ProjectDropdown/> : null}
</div>
</div>
</div>
);
};
setpList(pList);
})
// Then I simply render the component "pList"
return (
<div className="container">
<div className="projectsContainer">
<div className="projectsTitle">
Projects:
<div className="addProject" onClick={toggleShow}></div>
</div>
{pList}
<div className="message">
{message}
</div>
</div>
</div>
)
Ideal look would be this before user presses droparrow on project
And this afterwards, but only for the project clicked
You have to options to such problems:
1- make a state for each item in the list, and for each one you have a boolean that can tell if that item has its dropdown opened or not
2- make single state item, but make it a number that represents the clicked on item id, when that id matches the state you open the dropdown for that item
const [selectedItem, setSelectedItem] = useState(-1);
and in your jsx
<div className="projects" key={i}>
<div className="projectName">Name: <div className="projectText">{projectName}</div></div>
<div className="projectDate"> Date Created: <div className="projectText">{date}</div></div>
<div className="projectLabels">LabelsLeft:</div>
<div className="drop">
<div className="dropArrow" onClick{()=>setSelectedItem(i)}>
{selectedItem == i ? <ProjectDropdown /> : null}
</div>
</div>
</div>
And as an advice, it's preferable to add a real id taken from your API, because the key passed to each item is so important for react, not only for showing the dropdown.
Suggested solution code
function TestSolution() {
// Using hook to set display of dropdown to false
const [selectedItem, setSelectedItem] = useState(-1);
// This piece automatically renders clickable projects from database
const pList = project_list.map((projectName, i) => (
<div className="projects" key={i}>
<div className="projectName">Name: <div className="projectText">{projectName}</div></div>
<div className="projectDate"> Date Created: <div className="projectText">{date}</div></div>
<div className="projectLabels">LabelsLeft:</div>
<div className="drop">
<div className="dropArrow" onClick{() => setSelectedItem(i)}>
{selectedItem == i ? <ProjectDropdown /> : null}
</div>
</div>
</div>
))
// Then I simply render the component "pList"
return (
<div className="container">
<div className="projectsContainer">
<div className="projectsTitle">
Projects:
<div className="addProject" onClick={toggleShow}></div>
</div>
{pList}
<div className="message">
{message}
</div>
</div>
</div>
)
}
Thinking in react is different, don't use a new state for some data if you can derive it from another state or data, that will make your life easier.

React Hook not opening form onClick

I am trying to refactor from class based to functional. While doing so I will need to use hooks instead of setting this.state etc.. I am trying to get a FORM to open when i click a button. The button will also change from "add reply" to "submit comment" once the form opens. I am stumped. This is the best thing I could come up with... Doesnt work. in fact, it makes my "add reply" button completely disappear. Any thoughts on this? Here is the code that I have written. inside of the comment I am trying to return a component using ternary....
image of component as-is
import React, {useState} from 'react';
import FormOpen from './FormOpen';
const CommentCreated = (props) => {
const [resource, setResource] = useState([{visible: false, addReply: 'Add Reply'}]);
return (
<div className="ui threaded comments">
<h3 className="ui dividing header">Comments</h3>
<div className="comment">
<a href="/" className="avatar">
<img alt="avatar" src= {props.avatar} />
</a>
<div className="content">
<a href="/" className="author">{props.author}
</a>
<div className="metadata">
<span className="date">Today at 5:42PM</span>
</div>
<div className="text">{props.content}
</div>
<div className="actions">
{resource.visible ? <FormOpen /> : null}
<a className="reply" onClick={() => {setResource([{visible: true, addReply: 'Submit Comment'}]);}}>{resource.addReply}
</a>
<a className="save">Save</a>
<a className="hide">Hide</a>
</div>
</div>
</div>
</div>
);
};
export default CommentCreated;
You should replace the setResource(['Submit Comment', true]) with :
setResource([{
visible: true,
addReply: 'Submit Comment'
}])
To match the shape of your state.
EDIT:
Here is a working example based on your code.
As I can see from your example, your resource state doesn't need to be an array but simply an object.
Furthermore, as you are using hyperlinks <a />, you must use preventDefault() on your event being triggered if you don't want the page to refresh.

How can i split a column of list in different rows in bootstrap

I am using bootstrap with reactjs. I am getting data from a list of the array, and that data should be display on screen but when I tried I get a list of data in the vertical direction. I want to make Only 4 element in a row and then it should make another row like buttons in the calculator. I tried to do that using bootstrap grid but it did not work.
This is my code. Here I mapped through and passed data to different component
PadButtons = this.props.currentPadBank.map((drumObj, i, padBankArr) => {
return (
<PadButton
key={padBankArr[i].id}
clipId={padBankArr[i].id}
clip={padBankArr[i].url}
keyTrigger={padBankArr[i].keyTrigger}
keyCode={padBankArr[i].keyCode}
/>
);
Then here I display the data
<div id="display">
<button
className="drum-pad btn btn-secondary btn-lg"
onClick={this.playSound}
id={this.props.clipId}
>
<audio id={this.props.keyTrigger} src={this.props.clip} />
{this.props.keyTrigger}
</button>
</div>
This is my codesandbox Link
Make the following changes
In Drumpad.js
<div className="container">
<div className="row">{PadButtons}</div>
</div>
In PadButton.js
<div id={`display-${this.props.keyTrigger}`} className="col-sm-3">
<button
className="drum-pad"
onClick={this.playSound}
id={this.props.clipId}>
<audio id={this.props.keyTrigger} src={this.props.clip} />
{this.props.keyTrigger}
</button>
</div>
See updated https://codesandbox.io/s/7ymd7
Basically, you had to add col-sm-3 class on the element that is being repeated and it must be inside a wrapper row div.

ReactJs Create Reusable component with multiple different children

I am new to React and am trying to get up to speed on a few basic concepts, trying to shoehorn it into an existing application (with the intention of slowly converting the entire app into React). So this may be a vary basic question.
I am converting my dialogs first (my app has a bunch of bootstrap modal dialogs), so I am using react-modal library to create these dialogs. What I would like to do is make the outer markup for this dialog reusable across all my dialogs. For instance, with this render method:
render() {
return(
<Modal
className="Modal__Bootstrap modal-dialog"
closeTimeoutMS={150}
isOpen={this.state.modalIsOpen}
onRequestClose={this.handleModalCloseRequest}
>
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" onClick={this.handleModalCloseRequest}>
<span aria-hidden="true">×</span>
<span className="sr-only">Close</span>
</button>
*******INSERT HEADER COMPONENT*******
</div>
<div className="modal-body">
*******INSERT BODY COMPONENT*******
</div>
<div className="modal-footer">
<button type="button" className="btn btn-default" onClick={this.handleModalCloseRequest}>Cancel</button>
*******INSERT ADDITIONAL FOOTER BUTTONS COMPONENT*******
</div>
</div>
</Modal>
);
}
I would like to have the lines that start with the ******** to be variable components that are appropriate (and different) for each dialog.
Obviously, for each dialog, I could just copy and paste this render method into that dialog's class and have hard coded components for that dialog. Something like (for the body):
<div className="modal-body">
<DialogABody />
</div>
and:
<div className="modal-body">
<DialogBBody />
</div>
But if I do that, I am then duplicating all the markup for the dialog "chrome" in react.
Essentially, I need some sort of dynamic component?
Is this possible?
on your modal react class you want to do this
class Modal extends React.Component {
render() {
return (
<div>
{this.props.header ? <HeaderComponent text={this.props.header} /> : null }
{this.props.children}
{this.props.footer ? <FooterComponent text={this.props.footer} /> : null }
</div>
}
}
then when calling this pass your props
<Modal
className="Modal__Bootstrap modal-dialog"
closeTimeoutMS={150}
isOpen={this.state.modalIsOpen}
onRequestClose={this.handleModalCloseRequest}
header="My Header"
footer="My Footer"
>
as for the body component part you should pass that as a prop to this dialog class when calling it.
instead of this:
<div className="modal-body">
<DialogABody />
</div>
you should have this:
<div className="modal-body">
{this.props.dialogComponent}
</div>
which gets passed through on the parent to this class
Call your model dialog with the wanted components as props.
<model header={headerComponent} body={bodyComponent}/>
In model render just put {this.props.header} and so on.
for one child component you could simply use this.props.children, however, as you have 3 components, you need to put them into separate props:
<DialogBody header={<div>header</div>} body={<div>body</div>} footer={<div>footer</div>}/>

Resources