使用webpacker运行React和Rails 5.2。将反应组件的属性传递给rails后端
我在页面顶部有一个ElasticSearch搜索栏,我无法将正确的请求发送到后端,并且允许rails后端处理搜索请求。
我们现在没有准备好将此作为SPA的权利,但我似乎无法得到填充的参数。
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {asyncContainer, Typeahead} from 'react-bootstrap-typeahead';
const AsyncTypeahead = asyncContainer(Typeahead);
class SearchBar extends Component {
constructor(props) {
super(props)
this.state = {
options: ['Please Type Your Query'],
searchPath: '/error_code/search',
selected: [""],
}
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
searchErrorCodes(term) {
fetch(`/error_code/auto?query=${term}`)
.then(resp => resp.json())
.then(json => this.setState({options: json}))
}
handleChange(error_code_name) {
let newValue = error_code_name
this.setState({
selected: newValue
});
this.setState({
searchPath: `/error_code/search?error_code=${this.state.selected}`,
});
console.log(`This is the new searchPath is ${this.state.searchPath}`);
}
handleSubmit(e) {
alert(`submitted: ${this.state.selected}`);
// event.preventDefault();
}
render() {
return (
<div>
<form ref="form"
action={this.state.searchPath}
acceptCharset="UTF-8"
method="get"
onSubmit={e => this.handleSubmit(e)}
>
<AsyncTypeahead
onSearch={term => this.searchErrorCodes(term)}
options={this.state.options}
className="search"
onClick={e => this.handleSubmit(e)}
selected={this.state.selected}
onChange={e => this.handleChange(e)}
/>
<button
action={this.state.searchPath}
acceptCharset="UTF-8"
method="get"
type="submit"
className="btn btn-sm btn-search">
<i className="fa fa-search"></i>
</button>
</form>
</div>
)
}
}
ReactDOM.render(<SearchBar/>, document.querySelector('.search-bar'));
一切都呈现正确,但输入未正确发送到控制器。
当您触发'this.setState({searchPath:'/ error_code/search?error_code = $ {this.state.selected}', });'时,它会发送'this的旧值。 state.selected',而不是你刚刚设置的那个? – Denialos
据我可以告诉它得到更新,但当你点击提交时,表单按钮或某些东西不会传递该更新。并且它没有发送数据到后端 –
我会在你回答这个问题后添加到我的答案中。你如何从'onChange = {e => this.handleChange(e)}'获得'error_code_name'?在这种情况下,内联es6箭头功能是无关紧要的,并且还会导致性能问题并打破浅层比较。 –