イムランニングを終了webpackerと5.2を反応し、Railsの。レールに反応するコンポーネントから属性を渡すと、バック
私は、ページの上部にElasticSearch検索バーを持っている、と私はトラブルのバックエンドに適切なリクエストを送信した、とレールが戻って検索要求を処理するために終了できるようにしています。
私たちは何の場所の近くに、今SPAとしてこれを持って準備ができていないが、私はparamsを移入する得るように見える傾けます。
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
私はそれが更新を取得することができますが、送信ボタンをクリックすると、フォームのボタンや何かがその更新を渡さないことを伝えることができます。そして、それはバックエンドにデータを送信しません –
私はあなたの答えの後に私の答えに追加します。あなたは 'error_code_name'を' onChange = {e => this.handleChange(e)} 'からどのように取得していますか?インラインes6矢印関数はこの場合は無関係で、パフォーマンス上の問題を引き起こし、浅い比較を破ります。 –