绑定选择反应挂钩形式

时间:2020-05-03 11:26:29

标签: reactjs react-native react-redux react-hooks react-hook-form

https://codesandbox.io/s/dhananjay-rtb8r?file=/src/App.js

请查看示例代码。我想根据州来绑定城市

1 个答案:

答案 0 :(得分:1)

您可以使用Array.filter(),然后使用Array.map()创建子菜单。

Documentation here

示例:

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])