Uncaught Invariant Violation on rendering search data

别来无恙 提交于 2020-08-25 07:08:23

问题


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 use props.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: inputs 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!