score:6
set the value to empty as
handleClick = () => {
ReactDOM.findDOMNode(this.refs.form).value = "";
}
and yes onClick expects a function or a value not a string. Also React.findDOMNode()
is deprecated. You should use ReactDOM.findDOMNode();
class Hello extends React.Component {
handleChange = (e) => {
}
handleClick = () => {
ReactDOM.findDOMNode(this.refs.form).value = "";
}
render() {
return (
<div>
<input type="text" ref="form" placeholder="Search indicators" className="uk-width-1-1" onChange={this.handleChange} />
<button className="uk-button uk-width-1-1" onClick={this.handleClick}><i className="uk-icon-undo"></i>click</button>
</div>
)
}
}
ReactDOM.render(<Hello />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.js"></script>
<div id="app"></div>
Also Its a better idea to use state and change the state for input value.
as
class Hello extends React.Component {
constructor() {
super()
this.state = {
valueAttr : ''
}
}
handleChange = (e) => {
this.setState({valueAttr: e.target.value});
}
handleClick = () => {
this.setState({valueAttr: ''})
}
render() {
return (
<div>
<input type="text" ref="form" placeholder="Search indicators" className="uk-width-1-1" value = {this.state.valueAttr} onChange={this.handleChange} />
<button className="uk-button uk-width-1-1" onClick={this.handleClick}><i className="uk-icon-undo"></i>click</button>
</div>
)
}
}
ReactDOM.render(<Hello />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.js"></script>
<div id="app"></div>
I personally prefer the second approach. But you can do it in whichever way you feel comfortable.
score:0
Just use:
this.refs.form.getInputNode().value = '';
Should do the trick
score:1
Here onClick="React.findDOMNode(this.refs.form).reset()"
you are assigning string to onClick value of button, not a function, if you want to assign a function it should look like onClick={React.findDOMNode(this.refs.form).reset}"
or onClick={() => React.findDOMNode(this.refs.form).reset()}
Next, if i recall correctly, findDOMNode
is deprecated in React, so you should use ReactDOM for this.
And last, I doubt there is reset
method present, so I think you should try mutating input value directly.
Try this
onClick={() => this.refs.form.value = ''}
Source: stackoverflow.com
Related Query
- reseting or clearing an input field with reactjs
- Using an input field with onBlur and a value from state blocks input in Reactjs JSX?
- reactjs - redux form and material ui framework -- with auto type - and clearing field functionality
- Clearing an input field after submit with React
- ReactJS - Adding an Input field with each element and get value from it
- Can't enter text value in input field with ReactJS
- Adding and removing input fields in Reactjs with values not reseting
- Is there a way to add a cancel button with an input field in Sweetalert, Reactjs
- Submitting multiple input fields with same field name- ReactJs
- Clear an input field with Reactjs?
- Updating a React Input text field with the value on onBlur event
- Clear form input field values after submitting in react js with ant-design
- How to dynamically update the value for any input field with one event handler function, using hooks
- React rebuild tree with HOC components (causing input field focus loss)
- How do I programatically fill input field value with React?
- input field target is null while accessing in onChange field in reactjs
- Dynamically adding Input form field issue reactjs
- ReactJS clearing an input from parent component
- ReactJS instant Search with input
- How can I make my input field prefilled with data and editable on page load?
- Autosuggest on input of specific character with ReactJS
- yup validation to Validate formki field whether starts with value of other input field
- Initializing an input field with props value in react
- REACT- How do I raise a warning if a specific input field is empty with Yup?
- how to Stop rerendering of entire component onChange event on input text field in reactJs
- Reactjs Redux updating data values from the state/store with an input
- React how to make an input field with a fixed value combined with a dynamic value
- ReactJS - number only input with PropType set to number but gets initialized to zero
- Use React Dropzone Uploader with custom input field
- Input field (using downshift) is clearing the entered data when click on div other than submit and input field
More Query from same tag
- Can I use HOC to wrap component with graphql and redux
- Nodejs Passportjs question about which strategies to use for React frontend with facebook, google or email login
- How to filter request to Firebase realtime database
- Access constructor variables inside the render method - Reactjs
- Expose state and method of child Component in parent with React
- Submit Button On React Form Doesnt Run Submit Function
- How to pass register to React Hook Forms Factory Component?
- button onClick doesn't work when disabled=True is initialized (Reactjs)
- Get code coverage from Cypress with babel, nyc and istanbul plugin?
- Redux Saga watcher differences
- unable to pass data via onClick event react
- How to test child components from a PrefetchResourceContainer?
- How can I use formik's setSubmitting() method on onSubmit handler?
- How to pass one state the component father to component child to open a modal React
- React Big Calendar Add Date Picker Instead Of Today Button
- Location of static application settings
- Conditional inline style in react js
- Why is :read-only CSS pseudo-class being applied on this checkbox?
- JestJS: How to test for state value on catch()
- cancel all subscriptions and asynchronous tasks in a useEffect cleanup function - Redux Toolkit
- DOM Attributes and React
- Copy Props From Parent in ReactJs?
- React component: getting Syntax error: Cannot read properties of undefined (reading 'map') (0:undefined) when trying to add function to my code
- Is there any way to access `__filename` in Next.js?
- How to open a link if a function get called Django
- shortest way to create a comma separated object list
- File upload progress using react-dropzone
- React admin select input default empty value
- React useState hook - update state of a object
- Cannot read properties of undefined - javascript class