【发布时间】:2018-09-27 08:33:14
【问题描述】:
我是 React JS 的新手 我有 2 个下拉列表:国家和城市。 我想在静态 json 的帮助下显示基于国家的城市 有人可以帮我看看吗?
[
{
"id": "1",
"country": "India",
"cities": [
{
"id": "01",
"cityName": "Delhi"
},
{
"id": "02",
"cityName": "Mumbai"
},
{
"id": "03",
"cityName": "Pune"
}
]
},
{
"id": "2",
"country": "Canada",
"cities": [
{
"id": "01",
"cityName": "Toronto"
},
{
"id": "02",
"cityName": "Ottawa"
},
{
"id": "03",
"cityName": "Winnipeg"
}
]
}
]
import * as React from 'react'; import * as styles from './App.module.scss'; const App = () => { return ( <div className={styles.app}> <div> <span>Country: </span> <select> <option>Select Country</option> </select> </div> <div> <span>City: </span> <select> <option>Select Country</option> </select> </div> </div> ); }; export default App;
【问题讨论】:
-
import * as React from 'react';从 './App.module.scss' 导入 * 作为样式; const App = () => { return (); };导出默认应用;国家:城市:
-
我没有时间写完整的代码,但这是你应该做的。首先将json字符串转换为JObject,然后处理该对象以创建
Touple,其键值为(国家为键),然后(城市列表为值),然后将它们设置为状态。然后将该状态值绑定到组件并在更改国家/地区选择火处理程序时更新状态。
标签: json reactjs static dropdown