githubでリポジトリを検索できるようにするためにGithub Search APIを使用しています。私はReactを使用しており、これのすべてに全く新しいものです。 入力値をAJAXリクエストに渡して、特定の名前のリポジトリのみを要求します。 Reactを使って入力値を渡す方法はありますか?ReactでAJAXリクエストに入力値を渡す
class SearchBox extends React.Component {
constructor(props) {
super(props);
this.onKeyUp = this.onKeyUp.bind(this);
this.onChange = this.onChange.bind(this);
}
render() {
return(
<form>
<input type="text" className="searchbox" onChange={this.onChange} onKeyUp={this.onKeyUp} />
<ul className="suggestions">
<li>Filter for a city</li>
<li>or a state</li>
</ul>
</form>
);
}
onChange(event) {
const matchArray = findMatches(this.value, repositories);
const html = matchArray.map(place => {
const regex = new RegExp(this.value, "gi");
const cityName = place.city.replace(regex, `<span className="hl">${this.value}</span>`);
const stateName = place.state.replace(regex, `<span className="hl">${this.value}</span>`);
return `
<li>
<span className="name">${cityName}, ${stateName}</span>
</li>
`;
}).join('');
console.log(matchArray);
}
onKeyUp(event) {
const matchArray = findMatches(this.value, repositories);
const html = matchArray.map(place => {
const regex = new RegExp(this.value, "gi");
const cityName = place.name.replace(regex, `<span className="hl">${this.value}</span>`);
return `
<li>
<span class="nameName">${cityName}, </span>
</li>
`;
}).join('');
console.log(matchArray);
}
}
ReactDOM.render(
<SearchBox />,
document.getElementById("searching")
);
JS
const endpoint = 'https://api.github.com/search/repositories?sort=stars&order=desc&q=' + searchTerm;
const repositories = [];
fetch(endpoint)
.then(blob => blob.json())
.then(data => repositories.push(...data));
function findMatches(wordToMatch, repositories) {
return repositories.filter(place => {
const regex = new RegExp(wordToMatch, "gi");
return place.city.match(regex) || place.state.match(regex);
});
};
あなたが見ることができるようにだから私は、検索語と呼ばれる変数に入力値を渡したいです。
完全に正直なところ、私はあなたが何を意味するのか理解していません。これを試すと、値が定義されていないというエラーメッセージが表示されます。 – Naomi
@Naomi実例を含めるために私の答えを更新しました。 –
これは私の質問にも答えません。変数をAJAXリクエストに渡していません。 – Naomi