最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught Invariant Violation on rendering search data - Stack Overflow

programmeradmin2浏览0评论

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',
        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 ments are very appreciated. Thanks a lot!

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.',
        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 ments are very appreciated. Thanks a lot!

Share Improve this question edited Apr 11, 2016 at 2:25 Interrobang 17.4k3 gold badges56 silver badges63 bronze badges asked Apr 10, 2016 at 5:11 DonnaDonna 432 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15

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>

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/ponents/input-group/ Also, you can check this for the type of input: reactstrap Forms

发布评论

评论列表(0)

  1. 暂无评论