score:2
Accepted answer
here is the whole solve. there was an error in the filterresults
function.
import {usestate} from 'react'
import textfield from "@mui/material/textfield";
import iconbutton from "@mui/material/iconbutton";
import clearicon from '@mui/icons-material/clearoutlined'
export default function app() {
const [filteredlocations, setfilteredlocations] = usestate('');
const clearsearch = () => {
setfilteredlocations('')
};
const filterresults = (e) => {
setfilteredlocations(e.target.value);
};
return (
<div classname="app">
<textfield
placeholder="search locations"
value={filteredlocations}
onchange={filterresults}
inputprops={{
endadornment: (
<iconbutton onclick={clearsearch} edge="end">
<clearicon />
</iconbutton>
)
}}
/>
</div>
);
}
codesnadbox link - https://codesandbox.io/s/how-to-clear-textfield-in-react-tb73t
score:2
const [filteredlocations, setfilteredlocations] = usestate(locations);
const clearsearch = () => {
setfilteredlocations("");
};
const filterresults = (e) => {
....
setfilteredlocations(filteredlocations);
};
<textfield
value = {filteredlocations}
placeholder="search locations"
onchange={filterresults}
inputprops={{
endadornment: (
<iconbutton onclick={clearsearch} edge="end">
<clearicon />
</iconbutton>
)
}}
/>
user a state to keep the text. and use that value as textare value. change that state to empty inside clearsearch function.
Source: stackoverflow.com
Related Query
- How to clear TextField after ENTER key press in React
- How to override the width of a TextField component with react MUI?
- How to test a TextField Material-UI element with React Jest?
- How to clear input values of dynamic form in react
- How to clear a settimeout in react
- How can I clear the localstorage when user close react application window?
- How do I clear states or empty array on click in react 0.14 ES6?
- How to clear react state in modal after closing?
- How do I get my React TextField to open the number pad when inputting on a mobile device?
- How to do timeout and then clear timeout in react functional component?
- How to clear uncontrolled field in react
- React how to use icon inside Textfield Material-UI with TypeScript
- How to apply Form validation for React Material-UI TextField and Select?
- How to add a Clear Button on a Formik Textfield
- How to change material-ui Textfield label styles in react
- How do I pass in the variant property of the material-ui TextField from a wrapping React component
- How do I clear Bootstrap 4 errors after I submit my React form?
- React How to conditionally override TextField error color in Material-UI?
- How to center placeholder and text in React Material UI TextField
- React - How to clear the value of a React-Select when another React-Select changes
- How can I validate an Autocomplete multiple TextField using React Hook Form and Yup?
- How to clear multiple input values in React
- How to clear TextField in react?
- How to clear TextField of Materal ui
- how to open a textfield and set its value on select some value in dropdown in react
- how to make tooltip for TextField material ui react hooks
- How to remove arrow from React TextField Select
- How to get the TextField value with onclick button event React Material UI
- How to clear data in specific state index using react js
- How to clear a React Bootstrap textarea form control after the form is submitted?
More Query from same tag
- Conditionally rendering in React.js not working
- How do i hookup a React button to authenticate with Devise Token Auth + OmniAuth
- React useState passed as props is not changing in child components
- How to reset filter fields in React Table?
- ReactJS / Chatbot with hooks
- Why don't my mapped prop values update after dispatch
- Overwritting or changing package.json "homepage" value
- req.file is showing undefined in console
- React: How do I only render a component on the page once I click on it from a mapped array?
- How to modify useState array one by one in mapped promise
- Why is my state iterations being overwritten?
- Why closing a page does not unmount the component
- Is there a way to make Enter and Shift + Enter work the same way in TinyMCE?
- array in a react component is getting converted into a number(which is of length of that array) after pushing an object into it
- Mock a button click in React with Jest and Enzyme
- Download file with react failed even though the request was successful
- Use component function in React loop
- Unable to get property 'location' of undefined or null reference ReactJS
- Why can I not set this state in React?
- useState in react custom hook always calls the function
- Why my action is not triggering in the reducer?
- data not rendering using flat list
- React, getting could not proxy request error
- onLoad event of <img /> tag does't fire on initial page access
- State to open and close a Dialog in TypeScript in React
- deeplink in react router (hooks)
- Custom CSS dropdown to focus on dropdown elements
- React: Store JWT
- Diving into props while testing React using Enzyme
- React - Conditional mounting/unmounting of a component