[英]how to pass only those values which changed in POST request body
I have multiple state variables, that contains data entered in a form by the user.我有多个 state 变量,其中包含用户在表单中输入的数据。 Since this form is only meant to update the existing values, I have to pass in only those values that have changed from its initial value (the one returned from the GET request).由于此表单仅用于更新现有值,因此我必须仅传递那些已从其初始值(从 GET 请求返回的值)更改的值。
State: State:
const [name, setName] = useState(props.user?.name ?? null);
const [lang, setLang] = useState(props.user?.lang ?? null);
const [enableChecks, setEnableChecks] = useState(props.user?.checkEnabled ?? false)
In the event that the user only changed the name, how can I pass in only name in the request body?如果用户只更改了名称,我如何在请求正文中仅传递名称?
What I have tried: I have the user props, so I have multiple if statements that check if the props matches the state. If it doesn't, then I add it to the request payload.我尝试了什么:我有用户道具,所以我有多个 if 语句来检查道具是否与 state 匹配。如果不匹配,那么我将其添加到请求负载中。 This works, but when there's a lot of state, there will be a lot of if statements, which isn't nice to look at.这个可以,但是当state很多的时候,if语句会很多,不好看。
Is there a better way to do this?有一个更好的方法吗?
You can keep your state in an object, and then only update field state when the updatedUser
and user
state values are different.您可以将您的 state 保留在 object 中,然后仅在更新的用户和user
state 值不同时更新字段updatedUser
。
//use `import` in your real component instead //import { useState } from 'react'; const { useState } = React; //fake your user prop const userProp = { name: "Name", lang: "English", } function App(props) { const [user, setUser] = useState(props.user); const [updatedUser, setUpdatedUser] = useState({}); const handleChange = (e) => { const newlyUpdatedUser = {...updatedUser, } if(props.user[e.target.name] === e.target.value) { delete newlyUpdatedUser[e.target.name] } else { newlyUpdatedUser[e.target.name] = e.target.value } setUpdatedUser(newlyUpdatedUser); setUser({...user, [e.target.name]: e.target.value }) }; console.log(updatedUser) return ( <React.Fragment> <label> Name: <input value={user.name} name="name" onChange={handleChange} /> </label> <label> Lang: <input value={user.lang} name="lang" onChange={handleChange} /> </label> </React.Fragment> ); } ReactDOM.render(<App user={userProp} />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script> <div id="root"></div>
Instead of having multiple state variables, you can have a single state variable like您可以拥有一个 state 变量,而不是拥有多个 state 变量,例如
const [state, setState] = useState(props.user)
and then change handler should look like然后更改处理程序应该看起来像
const handleChange = (e) => {
setState({
...state,
[e.target.name]: e.target.value,
});
};
finally, when submitting the form you can make your body data for post request like最后,在提交表单时,您可以为发布请求制作您的身体数据,例如
const handleSubmit = () => {
const requestData = {}
for (const [key, value] of Object.entries(state)){
if(props.user[key] !== value) {
requestData[key] = value
}
}
axios.post('some api url', responseData)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.