그렇게처럼 드롭 다운을위한 작은 구성 요소를 작성하는 경우 :
import React, { Component } from 'react';
class SelectOption extends Component {
render() {
return (
<option value={this.props.dataItem.key}>{this.props.dataItem.val}</option>
)
}
}
class SimpleDropdown extends Component {
render() {
let options = [];
if (this.props.selectableData) {
const selectableData = this.props.selectableData;
options = selectableData.map((dataItem) =>
<SelectOption key={'option_' + dataItem.key} dataItem={dataItem} />
);
}
return (
<div>
<select onChange={this.props.handleInputChange} name={this.props.name} >
{options}
</select>
</div>
)
}
}
export default SimpleDropdown;
당신은 당신의 부모 구성 요소,이 같은에서 사용할 수
... 이런
import React, { Component } from 'react';
import SimpleDropdown from './common/SimpleDropdown';
class Parentextends Component {
componentDidMount() {
// here you handle your ajax call or calls, depending on what you choose to go with
}
handleInputChange = (e) => {
const target = e.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
render() {
const ajaxResultFirst = ajaxResult.First;
const ajaxResultSecond = ajaxResult.Second;
const ajaxResultThird = ajaxResult.Third;
const ajaxResultFourth = ajaxResult.Fourth;
return (
<section className="form">
<SimpleDropdown
name="FirstDropdown"
selectableData={ajaxResultFirst}
handleInputChange={this.handleInputChange}
/>
<SimpleDropdown
name="SecondDropdown"
selectableData={ajaxResultSecond}
handleInputChange={this.handleInputChange}
/>
<SimpleDropdown
name="ThirdDropdown"
selectableData={ajaxResultThird}
handleInputChange={this.handleInputChange}
/>
<SimpleDropdown
name="FourthDropdown"
selectableData={ajaxResultFourth}
handleInputChange={this.handleInputChange}
/>
</section>
);
}
};
export default Parent;
뭔가해야 작업. 하지만 jQuery와는 다른 플러그인을 사용하여 AJAX 요청을 작성하는 것이 좋습니다. 첫 번째 선택은 axios https://github.com/mzabriskie/axios입니다.
다른 장소에 저장하지 않는 한 하나의 Ajax 호출로 필요한 값을 가져온 다음 응용 프로그램 상태에 결과를 저장할 수 있어야합니다. – Drum
모든 드롭 다운 값이 서로 독립적입니까? 또한, 왜 reactjs와 JQuery를 사용합니까? –
예 모두 서로 다른 값입니다. Jquery는 아약스 호출이있는 단일 함수 일뿐입니다. – B77