https://codesandbox.io/s/dhananjay-rtb8r?file=/src/App.js
请查看示例代码。我想根据州绑定城市
您可以使用 Array.filter() ,然后 Array.map() 来创建您的子菜单。
例子 :
city.filter(city => city.scopeId === state.id).map(city => <MenuItem {...city} />)
编辑:
挂钩示例:
React.useEffect(() => {
const getData = async () => {
const result = await axios.get('api-url/?state=' + state)
// for post requests with params , i suggest you use qs library
// const result = await axios.post('api-url/',qs.stringify({state}))
return result
}
const city = getData()
setState(oldState => ({...oldState,city}))
},[state])