below is my code
<button
className="btn"
id={index}
onClick={() =>
`Make an API call`
)
}
>
{loader && index =={"Dont know how to add button ID here to show loader"}? (
<i class="fas fa-spinner fa-spin login-spin"></i>
) : (
"Add To Cart"
)}
</button>
I have a bunch of buttons and i am planning to show loader if the user click on particular button,at the same time i am posting a request to backend, i have given index as id to each button but i am unable to access the button ID to show the loader symbol,
i do not know how to acces BTN id without click
PS:i am inside a function not a react component
You can make use of the below approach to achieve this
Define a state to set the selected button id, something like
selectedButtonId
On clicking the button, it should be set to the selected button id
and then after your API call is success, it should be reset to the
default value.
Then compare the index(id) of button with the state defined in
step(1) to show the loader as applicable.
Here is a sample code which utilizes the above points - But just changes the button label instead of a loading icon
export default function App() {
const [selectedBtnId, setSelectedBtnId] = useState(-1);
const buttons = [{ id: 1 }, { id: 2 }, { id: 3 }];
const handleClick = id => setSelectedBtnId(id);
return (
<div>
{buttons.map(({ id }) => (
<button id={id} key={id} onClick={() => handleClick(id)}>
{selectedBtnId === id ? 'Loading': 'Click Me'}
</button>
))}
</div>
);
}
Access the button id from the onClick event object.
Example:
const clickHandler = e => {
const { id } = e.target;
// id is button id
};
<button id={0} onClick={clickHandler}>Click Me</button>
Combine this with some loading state that correlates a currently loading button id. Use the current loading state button id to match the id for any specific button.
const [loadingId, setLoadingId] = useState({});
...
setLoadingId((ids) => ({
...ids,
[id]: true
}));
...
<button type="button" id={0} onClick={clickHandler}>
{loadingId[0] ? <Spinner /> : "Button Text Here"}
</button>
Full example:
const mockFetch = () =>
new Promise((resolve) => {
setTimeout(() => resolve(), 3000);
});
function App() {
const [loadingId, setLoadingId] = useState(null);
const clickHandler = async (e) => {
const { id } = e.target;
setLoadingId(ids => ({
...ids,
[id]: true
}));
try {
await mockFetch();
} catch {
// ignore
} finally {
setLoadingId(ids => ({
...ids,
[id]: false
}));
}
};
return (
<div className="App">
<button type="button" id={0} onClick={clickHandler}>
{loadingId[0] ? "loading..." : 0}
</button>
<button type="button" id={1} onClick={clickHandler}>
{loadingId[1] ? "loading..." : 1}
</button>
<button type="button" id={2} onClick={clickHandler}>
{loadingId[2] ? "loading..." : 2}
</button>
<button type="button" id={3} onClick={clickHandler}>
{loadingId[3] ? "loading..." : 3}
</button>
<button type="button" id={4} onClick={clickHandler}>
{loadingId[4] ? "loading..." : 4}
</button>
</div>
);
}
Demo
I would better go with a separate component for a button that receives an index and does whatever it wants
function LoadingButton({id}) {
const [isLoading,setIsLoading] = useState(false);
if (isLoading) return (<i class="fas fa-spinner fa-spin login-spin"></i>)
return (
<button
className="btn"
id={id}
onClick={() =>
setIsLoading(true)
MakeApiRequest().then(result=>setIsLoading(false))
)
}
>
"Add to Cart"
</button>
)
}
This is a better option because it allows you to have multiple standalone loading buttons, you can click some of them and each will show a spinner.
Then you can use it like
<LoadingButton id={index}/>
I guess you can just add it directly and judge that it meets the conditions for displaying the loading icon.
I want to disable the button and show the spinner when user clicks on the button. The code is below:
constructor(props) {
super(props);
this.state = {
username: '',
password: '',
submitted: false,
loading: false,
error: ''
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleSubmit(event) {
this.setState({ submitted: true });
this.setState({ loading: true });
...
}
render(){
return(
...
<Button type="submit" className="btn btn-primary btn-lg" disabled={this.state.loading}>
Login
</Button>
{
this.state.loading &&
<img alt="" src="loading.gif" />
}
... )
The problem here is that when I add in the src the favicon.ico it renders it, but if I try to add any image (.jpg or .gif) nothing happens. I also have the .gif in the same folder of the LoginPage.js. What am I missing here?
you need to import your image before using it :
import spinner from "path/loading.gif"
and then render like this :
<img alt="" src={spinner} />
Hello i have a question if some one could help.
I created a react app with tree buttons when i click on every button the code shows and hide text.
But i wanted when i click for example on button 2 the text from button 1 and 3 to be hidden. And the same for every button if i click on button 3 the text from button 1 and 2 to be hidden also.
import React, { useState } from 'react';
export default class Tinfo extends React.Component{
constructor(props){
super(props);
this.state = { show: new Array(3).fill(false) };
this.baseState = this.state
}
resetState = () => {
this.setState(this.baseState)
}
toggleDiv = (index) => {
var clone = Object.assign( {}, this.state.show );
switch(clone[index]){
case false:
clone[index] = true
break;
case true:
clone[index] = false
break;
}
this.setState({ show: clone });
}
render(){
return(
<div>
{ this.state.show[0] && <div id="tinfo"> First Div </div>}
{ this.state.show[1] && <div id="tinfo"> Second Div </div>}
{ this.state.show[2] && <div id="tinfo"> Third Div </div> }
<button onClick={() => this.toggleDiv(0)}>button 1</button>
<button onClick={() => this.toggleDiv(1)}>button 2</button>
<button onClick={() => this.toggleDiv(2)}>button 3</button>
</div>
)
}
}
since only one can be shown, then just reset the state
toggleDiv = index => {
const show = new Array(3).fill(false);
show[index] = true;
this.setState({
show
});
}
although this should now be named showDiv as it sets the state and hides the rest, it's not a toggle.
I have file upload input:
<input onChange={this.getFile} id="fileUpload" type="file" className="upload"/>
And I handle upload this way:
getFile(e) {
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = (theFile) => {
var data = {
blob: theFile.target.result, name: file.name,
visitorId: this.props.socketio.visitorId
};
console.log(this.props.socketio);
this.props.socketio.emit('file-upload', data);
};
reader.readAsDataURL(file);
}
If I upload same file twice, then upload event is not fired. How can I fix that? For simple js code it was enough to do the following: this.value = null; in change handler. How can I do it with ReactJS?
I think you can just clear the input value like this :
e.target.value = null;
File input cannot be controlled, there is no React specific way to do that.
Edit For old browsers (<IE11), you can use one of the following techniques.
See http://jsbin.com/zurudemuma/1/edit?js,output (tested on IE10 & 9)
What worked for me was setting a key attribute to the file input, then when I needed to reset it I update the key attribute value:
functionThatResetsTheFileInput() {
let randomString = Math.random().toString(36);
this.setState({
theInputKey: randomString
});
}
render() {
return(
<div>
<input type="file"
key={this.state.theInputKey || '' } />
<button onClick={this.functionThatResetsTheFileInput()} />
</div>
)
}
That forces React to render the input again from scratch.
This work for me - ref={ref => this.fileInput = ref}
<input id="file_input_file" type="file" onChange={(e) => this._handleFileChange(e)} ref={ref=> this.fileInput = ref} />
then in my case once the file was uploaded to the server , I clear it by using the statement below
this.fileInput.value = "";
I do it by updating key inside my file input.
This will force a re-render and previously selected file will go away.
<input type="file" key={this.state.inputKey} />
Changing the state inputKey will re-render the component.
One way to change the inputKey will be to always set it to Date.now() on click of a button which is supposed to clear the field.
With every click onClick you can reset the input, so that even with the same file onChange will be triggered.
<input onChange={this.onChange} onClick={e => (e.target.value = null)} type="file" />
import React, { useRef } from "react";
export default function App() {
const ref = useRef();
const reset = () => {
ref.current.value = "";
};
return (
<>
<input type="file" ref={ref} />
<button onClick={reset}>reset</button>
</>
);
}
The following worked for me using React Hooks. This is done using what is known as a "controlled input". That means, the inputs are controlled by state, or their source of truth is state.
TL;DR Resetting the file input was a two-step process using both the useState() and useRef() hooks.
NOTE: I also included how I reset a text input in case anyone else was curious.
function CreatePost({ user }) {
const [content, setContent] = React.useState("");
const [image, setImage] = React.useState(null); //See Supporting Documentation #1
const imageInputRef = React.useRef(); //See Supporting Documentation #2
function handleSubmit(event) {
event.preventDefault(); //Stop the pesky default reload function
setContent(""); //Resets the value of the first input - See #1
//////START of File Input Reset
imageInputRef.current.value = "";//Resets the file name of the file input - See #2
setImage(null); //Resets the value of the file input - See #1
//////END of File Input Reset
}
return (
<div>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="Add Post Content"
onChange={event => setContent(event.target.value)}
value={content} //Make this input's value, controlled by state
/>
<input
type="file"
onChange={event => setImage(event.target.files[0])} //See Supporting Doc #3
ref={imageInputRef} //Apply the ref to the input, now it's controlled - See #2
/>
<button type="submit">Submit Form</button>
</form>
</div>
)
};
Supporting Documentation:
useState Hook
Returns a stateful value, and a function to update it.
useRef Hook
If you pass a ref object to React, React will set its current property to the corresponding DOM node whenever that node changes.
Using files from web apps
If the user selects just one file, it is then only necessary to consider the first file of the list.
You can also include this in your input element if you know you are not going to be using the built-in file input value at all.
<input value={""} ... />
This way the value is always reset to the empty string on render and you don't have to include it awkwardly in an onChange function.
I know file input is always uncontrolled however the following code still works in my own porject, I can reset the input with no problems at all.
constructor(props) {
super(props);
this.state = {
selectedFile: undefined,
selectedFileName: undefined,
imageSrc: undefined,
value: ''
};
this.handleChange = this.handleChange.bind(this);
this.removeImage = this.removeImage.bind(this);
}
handleChange(event) {
if (event.target.files[0]) {
this.setState({
selectedFile: event.target.files[0],
selectedFileName: event.target.files[0].name,
imageSrc: window.URL.createObjectURL(event.target.files[0]),
value: event.target.value,
});
}
}
// Call this function to reset input
removeImage() {
this.setState({
selectedFile: undefined,
selectedFileName: undefined,
imageSrc: undefined,
value: ''
})
}
render() {
return (
<input type="file" value={this.state.value} onChange={this.handleChange} />
);
}
We can reset file input by using key = {this.state.fileInputKey} and initialsing fileInputKey to Date.now() in constructor state.
On file upload success , we need to again assign fileInputKey: Date.now(), so it will have different value than previous and it create new file input component on next render()
We can also do this manually by clicking button to clear/reset file Input
Below is the working code :
import React from "react";
import { Button } from "reactstrap";
class FileUpload extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedFile: null,
fileInputKey: Date.now(),
message: ""
};
this.handleClear = this.handleClear.bind(this);
this.onClickHandler = this.onClickHandler.bind(this);
this.onChangeHandler = this.onChangeHandler.bind(this);
}
onChangeHandler = event => {
this.setState({
selectedFile: event.target.files
});
};
onClickHandler = () => {
if (this.state.selectedFile === null) {
this.setState({
message: "Please select File"
});
return;
}
//axios POST req code to send file to server
{
/**
const data = new FormData()
data = this.state.selectedFile[0]
axios.post("http://localhost:8080/api/uploadFile/", data)
.then(res => {
if (res.status == 200) {
// upload success
}
})
.catch(err => {
//message upload failed
})
*/
}
//after upload to server processed
this.setState({
selectedFile: null,
fileInputKey: Date.now(),
message: "File Uploaded"
});
};
handleClear() {
this.setState({
selectedFile: null,
fileInputKey: Date.now(),
message: ""
});
}
render() {
return (
<div>
<input
type="file"
key={this.state.fileInputKey}
class="form-control"
onChange={this.onChangeHandler}
/>
<button
type="button"
class="btn btn-success btn-block"
onClick={this.onClickHandler}
>
Upload
</button>
<Button
type="button"
value="Clear"
data-test="clear"
onClick={this.handleClear}
>
{" "}
Clear{" "}
</Button>
<br />
<label>{this.state.message}</label>
</div>
);
}
}
export default FileUpload;
Here is my solution using redux form
class FileInput extends React.Component {
constructor() {
super();
this.deleteImage = this.deleteImage.bind(this);
}
deleteImage() {
// Just setting input ref value to null did not work well with redux form
// At the same time just calling on change with nothing didn't do the trick
// just using onChange does the change in redux form but if you try selecting
// the same image again it doesn't show in the preview cause the onChange of the
// input is not called since for the input the value is not changing
// but for redux form would be.
this.fileInput.value = null;
this.props.input.onChange();
}
render() {
const { input: { onChange, value }, accept, disabled, error } = this.props;
const { edited } = this.state;
return (
<div className="file-input-expanded">
{/* ref and on change are key properties here */}
<input
className="hidden"
type="file"
onChange={e => onChange(e.target.files[0])}
multiple={false}
accept={accept}
capture
ref={(input) => { this.fileInput = input; }}
disabled={disabled}
/>
{!value ?
{/* Add button */}
<Button
className="btn-link action"
type="button"
text="Add Image"
onPress={() => this.fileInput.click()}
disabled={disabled}
/>
:
<div className="file-input-container">
<div className="flex-row">
{/* Image preview */}
<img src={window.URL.createObjectURL(value)} alt="outbound MMS" />
<div className="flex-col mg-l-20">
{/* This button does de replacing */}
<Button
type="button"
className="btn-link mg-b-10"
text="Change Image"
onPress={() => this.fileInput.click()}
disabled={disabled}
/>
{/* This button is the one that does de deleting */}
<Button
type="button"
className="btn-link delete"
text="Delete Image"
onPress={this.deleteImage}
disabled={disabled}
/>
</div>
</div>
{error &&
<div className="error-message"> {error}</div>
}
</div>
}
</div>
);
}
}
FileInput.propTypes = {
input: object.isRequired,
accept: string,
disabled: bool,
error: string
};
FileInput.defaultProps = {
accept: '*',
};
export default FileInput;
In my case I had a functional component and after selecting a file it suppose to set the file name in the state so using any solution above was failing except the ref one which i fixed like this.
const fileUpload = props => {
const inputEl = useRef(null)
const onUpload = useCallback(e => {
uploadFile(fileDetails)
.then(res => {
inputEl.current.value = ''
})
.catch(err => {
inputEl.current.value = ''
})
})
return (
<input type='file' ref={inputEl} onChange={handleChange} />
<Button onClick={onUpload}>Upload</Button>
)
}
I recently got stumbled into this issue to reset the File type input field. I think it is still a milestone for most developers. So I thought I should share my solution.
Since we are listening to the onChange event to update the image file into some of our states, we will have our component rerendered once we set the state. In such case, we can specify the value of the input file as empty like value='' which will cause the input field to reset its value after each change of its value.
<input
type="file"
value=''
onChange={onChangeFnc}
/>
I want to pass this (the element) - a button to my controller by ng-disable.
here is my HTML:
<button type ="button" class = "btn btn-default" ng-click= "open()" ng-disabled = "checkRowId(this)">
</button>
And my controller :
$scope.checkRowId = function(btn){
console.log(btn); //undefined
}
The log shoes undefined, is there a way to pass a element like a button via ng-disabled?
There is no direct way to pass element via ng-disabled. You can create one directive say "disabled-ele" and put your element disabling logic there.
Example Code:
.directive('disabledEle', function() {
return {
restrict: 'EA',
link: function(scope, element, attrs) {
if(attrs.disabledEle.toLowerCase() === 'true') {
element.attr('disabled', 'disabled');
} else {
element.removeAttr('disabled');
}
}
}
});
<button type ="button" value="Click" class = "btn btn-default" disabled-ele="false">Click
</button>