gpt4 book ai didi

javascript - 渲染搜索数据时未捕获的不变违规

转载 作者:行者123 更新时间:2023-11-30 07:02:05 24 4
gpt4 key购买 nike

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

  • 将输入设置为参数
  • 呈现我从服务器获取的数据

我在玩的时候遇到了错误信息

Uncaught Invariant Violation input is a void element tag and must not have children or use props.dangerouslySetInnerHTML.

这是我的代码:

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 的新手。请按照您的意愿告诉我,非常感谢您的提示和评论。非常感谢!

最佳答案

错误很明显:input s 必须是空元素;也就是说,它们必须是自动关闭的。

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

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

或:<button>Search</button>

关于javascript - 渲染搜索数据时未捕获的不变违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36526061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com