呈现搜索数据时出现未捕获的不变冲突

Uncaught Invariant Violation on rendering search data

本文关键字:冲突 搜索 数据      更新时间:2024-04-26

我正在尝试使用React实现搜索。我的逻辑流程有两个问题:

  • 将输入设置为参数
  • 渲染我从服务器获得的数据

当我玩它时,我遇到了错误消息

Uncaught Invariant Violation输入是void元素标记,不能具有CCD_ 1或使用CCD_。

这是我的代码:

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);

我是React的新手。请按你的意愿给我建议,非常感谢你的建议和意见。非常感谢!

错误非常明显:inputs必须是void元素;也就是说,它们必须是自我封闭的。

此语法无效:<input type="button">Search</input>

您想要任一:<input type="button" value="Search" />

或者:<button>Search</button>

输入是自关闭标记,但是在某些情况下可以使用<Input></Input>您可以安装reactstrap包,然后导入并使用<Input></Input>标记,而不是使用<input></input>。此外,您还可以查看链接:https://reactstrap.github.io/components/input-group/此外,您还可以检查输入类型:reactstrap Forms