问题
I am trying to implement search using React. I have 2 problems on my logic flow:
- To set input as Params
- To render the data I get from server
While I'm playing with it, I have encountered the error message
Uncaught Invariant Violation input is a void element tag and must not have
children
or useprops.dangerouslySetInnerHTML
.
And this is my code:
import React, { PropTypes } from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Home.scss';
import AWS from 'aws-sdk';
var GetTech = React.createClass({
render: function() {
var createItem = function(item) {
var csd = new AWS.CloudSearchDomain({
endpoint: 'mycloudsearch.amazonaws.com',
region: 'us-east-1'
});
var params = {
query: {this.state.text}
}
csd.search(params, function (err, data) {
if (err) console.log(err, err.stack);
else {
console.log(JSON.stringify(data));
}
});
}
return (
{this.props.items.map(crateItem)}
)
}
});
var FilteredTechs = React.createClass({
getInitialState: function() {
return {
text: '',
items: []
};
},
handleChange: function(event) {
console.log(event);
this.setState({
text: event.target.value
});
},
handleSearch: function(event) {
event.preventDefault();
this.setState({
items: this.props.items,
text: ''
});
},
render: function() {
return (
<div>
<form onSubmit={this.handleSearch}>
<input
type="text"
value={this.state.text}
onChange={this.handleChange}
/>
<input type="button">Search</input>
</form>
<GetTech items={this.state.items} />
</div>
);
}
});
function Home({ techs }) {
<FilteredTechs />
}
Home.propTypes = {
techs: PropTypes.arrayOf(PropTypes.shape({
})).isRequired,
};
export default withStyles(Home, s);
I am new to React. Please advise me as you wish and your tips and comments are very appreciated. Thanks a lot!
回答1:
The error is pretty clear: input
s must be void elements; that is, they must be self-closing.
This syntax is invalid: <input type="button">Search</input>
You want either: <input type="button" value="Search" />
Or: <button>Search</button>
回答2:
Input is self-closing tag, However there some cases in which you can use <Input></Input>
You can install reactstrap package then import and use <Input></Input>
tag instead of using <input></input>
.
Also, you can check the link: https://reactstrap.github.io/components/input-group/
Also, you can check this for the type of input: reactstrap Forms
来源:https://stackoverflow.com/questions/36526061/uncaught-invariant-violation-on-rendering-search-data