1.在utils的index.js中写入:
export const isFalsy = (value) => value === 0 ? false : !value
export const cleanObject = (object: any) => {
const result = { …object }
Object.keys(result).forEach(key => {
const value = result[key]
if(isFalsy(value)) {
delete result[key]
}
})
return result
}
2.引入qs(npm 仓库所管理的包)
yarn add qs
3.使用
import { cleanObject } from ‘…/…/utils’
import * as qs from ‘qs’
const [param, setParam] = useState({
name: ‘’,
personId: ‘’
})
useEffect(() => {
console.log(‘apiUrl:’, apiUrl)
fetch(${apiUrl}/projects?${qs.stringify(cleanObject(param))}
).then(async res => {
if (res.ok) setList(await res.json())
})
}, [param])