'Can't type in React input text field
I'm trying my first bit of React.js and am stumped early on... I have the code below, which renders a search form into <div id="search"></div>. But typing in the search box does nothing.
Presumably something is going missing passing the props and state up and down, and this seems like a common problem. But I'm stumped - I can't see what's missing.
var SearchFacet = React.createClass({
handleChange: function() {
this.props.onUserInput(
this.refs.searchStringInput.value
)
},
render: function() {
return (
<div>
Search for:
<input
type="text"
value={this.props.searchString}
ref="searchStringInput"
onchange={this.handleChange} />
</div>
);
}
});
var SearchTool = React.createClass({
render: function() {
return (
<form>
<SearchFacet
searchString={this.props.searchString}
onUserInput={this.props.onUserInput}
/>
<button>Search</button>
</form>
);
}
});
var Searcher = React.createClass({
getInitialState: function() {
return {
searchString: ''
}
},
handleUserInput: function(searchString) {
this.setState({
searchString: searchString
})
},
render: function() {
return (
<div>
<SearchTool
searchString={this.state.searchString}
onUserInput={this.handleUserInput}
/>
</div>
);
}
});
ReactDOM.render(
<Searcher />,
document.getElementById('searcher')
);
(Eventually I will have other types of SearchFacet* but I'm just trying to get this one working.)
Solution 1:[1]
Using value={whatever} will make it so you cannot type in the input field. You should use defaultValue="Hello!".
See https://facebook.github.io/react/docs/uncontrolled-components.html#default-values
Also, the onchange should be onChange as @davnicwil points out.
Solution 2:[2]
defaultValue instead of value worked for me .
Solution 3:[3]
For me the following simple change worked perfectly
<input type="text"
value={props.letter}
onChange={event => setTxtLetter(event.target.value)} /> {/* does not work */}
change... value={myPropVal} to... defaultValue={myPropVal}
<input type="text"
defaultValue={props.letter}
onChange={event => setTxtLetter(event.target.value)} /> {/* Works!! */}
Solution 4:[4]
This might be caused by the onChange function is not updating the proper value which is mentioned in the input.
Example:
<input type="text" value={this.state.textValue} onChange = {this.changeText}></input>
changeText(event){
this.setState(
{textValue : event.target.value}
);
}
in the onChange function update the mentioned value field.
Solution 5:[5]
I also have same problem and in my case I injected reducer properly but still I couldn't type in field. It turns out if you are using immutable you have to use redux-form/immutable.
import {reducer as formReducer} from 'redux-form/immutable';
const reducer = combineReducers{
form: formReducer
}
import {Field, reduxForm} from 'redux-form/immutable';
/* your component */
Notice that your state should be like state->form otherwise you have to explicitly config the library also the name for state should be form.
see this issue
Solution 6:[6]
In a class component context...
If the changeHandler method is a normal function:
handleChange(e){
this.setState({[e.target.name]:[e.target.value]});
}
it can be used such as this...onChange={(e)=>this.handleChange(e)}
<input type="text" name="any" value={this.state.any} onChange={(e)=>this.handleChange(e)}></input>
If the changeHandler method is an arrow function:
handle = (e) =>{
this.setState({[e.target.name]:[e.target.value]});
}
it can be used like this... onChange={this.handle}
<input type="text" name="any2" value={this.state.any2} onChange={this.handle} ></input>
And this solved my "Can't type in React input text field" problem.
Solution 7:[7]
Instead of value use defaultValue
We use value when the fields are empty, but if we have to type or update the value from the value already in the input box. We must use defaultValue
Solution 8:[8]
Once I ran into a similar error. Let me describe it.
Edit.js
// components returns edit form
function EditVideo({id}) {
.....
// onChange event listener
const handleChange = (e) => {
setTextData({
...textData,
[e.target.name]: e.target.value.trim()
});
}
....
...
}
)
ImportEdit.js
import Edit from './Edit';
function ImportEdit() {
......
...
return (
<div>
<EditVideo id={id}/>
</div>
)
}
export default ImportEdit
The Problem was: I was unable to use spacebar (i.e. if i press spacekey, i didn't see space input)
The Bug: .trim()
.trim() method was trimming all the white space i typed
Note: Edit.js worked fine when used sepeartely without import
Solution 9:[9]
Give the onChange to the searchTool.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
