score:0
Accepted answer
<input type="file"/>
also is a input. So you can use<label>
tag and style it look like a button. In this case, I use image instead.I'm using
hidden
attribute to make the actual<input />
invisible.
<input
type="file"
id="avatar"
onChange={fileSelectedHandler}
hidden
/>
<label className={profileStyle.uploadLabel} htmlFor="avatar">
<Image
width="25px"
height="25px"
src="/icons/upload-image-icon.jpeg"
alt="upload-avatar"
/>
</label>
Here is what you want:
import React, { useState } from "react";
export const Upload = () => {
const [filename, setFilename] = useState("");
return (
<>
<input
value={filename}
type="text"
style={{
width: 200,
height: 28,
padding: 0,
border: "1px solid #767676"
}}
/>
<input
type="file"
id="upload"
hidden
onChange={(e) => {
// You have your file detail in e.tartget.files
console.log(e.target.files[0]);
setFilename(e.target.files[0].name);
}}
/>
<label
htmlFor="upload"
style={{
width: 90,
background: "#767676",
color: "#fff",
padding: 5
}}
>
Browse
</label>
</>
);
};
Check this Demo
Source: stackoverflow.com
Related Query
- How to create an input formfield with button that uploads a file using react and javascript?
- How to create a form with text and file input in react and send form data to server using Nodejs?
- How can i restrict user from not selecting other file types using input type file with react and typescript?
- How to create a div with arrow and divider for content such that they are aligned using react and css?
- How can I find and click a button that has no text using React Testing Library?
- How can I create a button which Fetches image from API and download to the local with using ReactJs
- How to Create a dropdown Button with see more option and on click toggle should not happen in react js?
- REACT: How to create a reusable custom hook with functions and effects that can change & react to changes in a host component's data
- how to test button that call submit form using jest and react testing library
- How to Create 3(country,state and city) dropdowns that populates data with each other in react js
- How to click a button and navigate to a new route with URL params using React Router 4
- How to create a local react library project with common components, and import that project locally
- How to let the user input number and increase it with a button in React JS
- How to create a signup form with React and Why won't a function be executed when called insight a form after input field?
- How to add a new textfield on button click and add integer value of that input field to an array in react
- React and progress bar. How can I add progress bar that will work with my input and my data
- How to create an excel file in react js with custom headings and styles while doing export to excel?
- How to Uncheck all checkboxes with a button. Input tag in child and button in parent. react - table
- How to map through array of objects and create a new array of objects with a new added property using react and javascript?
- using react js to create a website that could read .csv file and show in chartjs
- How to assign arrays student's id with Boolean value in react js and if i clicked on button again back to boolean value for that particular student
- How can I add a user input with an output and be able to display using React Javascript/ Material UI
- How do I create image maps (clickable areas on an image using <map >) with React and JSX?
- How to store Configuration file and read it using React
- How do I store JWT and send them with every request using react
- How to create forms with React and Rails?
- How to unit test a react event handler that contains history.push using Jest and Enzyme?
- How to inject port and host using ENV variable in Create React App Proxy settings?
- react native post form data with object and file in it using axios
- How to read and upload a file in reactjs using custom button
More Query from same tag
- Use ErrorBoundary together with asynchronous lifecycle-functions
- How to show first & last data point on amchart4 Line(XY) chart
- React's async with enzyme and expect
- React Spring - Animate list - one item in list at a time
- How to extend React class in typescript
- What is the best practice for getting initial data for a react/redux application?
- Can't input on other Form Fields
- How to type ReactNode that may have props?
- Component with boolean value
- Dynamically change font-size to fit text into container
- Is there a valid way to use redux with react without react-redux?
- Sum and sort with Ramda.js
- React loading screen before fetching API call
- antd - ant design table with pagination control supporting a widget to choose rows per page?
- React GridList w/ Modal Images
- Validation problem if an input is empty or not with React and React Bootstrap
- Why event.target.value is not updated after running await?
- Force update `this.props.location.search` on React Router
- Route inside Route - nesting routes - react.js
- Centering Semantic UI Column Contents
- Warnings about peer dependencies when running npm install
- React useEffect has missing dependencies
- Having trouble deploying React-Express app to Heroku
- ReactJS: How to create a transparent page?
- Error "No 'Access-Control-Allow-Origin' header is present on the requested resource" even after setting headers on server and client
- Customize menu item of material ui
- Add Icon in dangerously set Inner html in React
- React serves default content for incorrect paths
- Unexpected token < in JSON when using fetch()
- ReactJs - Fetch a quote every 5 seconds with axios and display it in the <p>