admin管理员组文章数量:1026125
If I have a ponent that's similar to the below:
import React, { useState } from "react";
import ReactDOM from "react-dom";
function calculateNewValueAndMaybeError(test) {
try {
// do something with test that might error
// const newTest = functionThatMightError(...)
// if it doesn't error, return { value: newTest, error: '' }
} catch (err) {
// is it correct to now return this in case of error?
// return { value: test.value, error: 'Something went wrong' }
}
}
function App() {
const [test, setTest] = useState({ value: "", error: "" });
return (
<div>
<p>Test: {test.value}</p>
<p>Error: {test.error}</p>
<button onClick={() => setTest(calculateNewValueAndMaybeError(test))}>
Click me
</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
And I have a button that should mutate the state of my variable called test
, is there a way to handle errors without having to pass around the object with both value and error inside all the time?
Otherwise, doesn't this result in necessary code sometimes like the below:
onChange={() => setTest({ value: e.target.value, error: test.error })}
Where you want to update the value of test.value
but retain the value of test.error
.
I feel like there's something obvious that I'm missing.
Thanks!
If I have a ponent that's similar to the below:
import React, { useState } from "react";
import ReactDOM from "react-dom";
function calculateNewValueAndMaybeError(test) {
try {
// do something with test that might error
// const newTest = functionThatMightError(...)
// if it doesn't error, return { value: newTest, error: '' }
} catch (err) {
// is it correct to now return this in case of error?
// return { value: test.value, error: 'Something went wrong' }
}
}
function App() {
const [test, setTest] = useState({ value: "", error: "" });
return (
<div>
<p>Test: {test.value}</p>
<p>Error: {test.error}</p>
<button onClick={() => setTest(calculateNewValueAndMaybeError(test))}>
Click me
</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
And I have a button that should mutate the state of my variable called test
, is there a way to handle errors without having to pass around the object with both value and error inside all the time?
Otherwise, doesn't this result in necessary code sometimes like the below:
onChange={() => setTest({ value: e.target.value, error: test.error })}
Where you want to update the value of test.value
but retain the value of test.error
.
I feel like there's something obvious that I'm missing.
Thanks!
Share Improve this question asked Mar 7, 2019 at 20:19 SudoscienceSudoscience 3013 silver badges11 bronze badges1 Answer
Reset to default 3That there's a need to merge two states suggests that they should be either handled as different states:
function App() {
function calculateNewValueAndMaybeError(test) {
try {
...
setValue(...);
setError(null);
} catch (err) {
setError('Something went wrong');
}
}
const [error, setError] = useState(null);
const [value, setValue] = useState('');
return (
<div>
<p>Test: {value}</p>
<p>Error: {error}</p>
<button onClick={() => setTest(calculateNewValueAndMaybeError(test))}>
Click me
</button>
</div>
);
}
Or custom state hook could be used to merge error
field:
const useErrorState = initialState => {
const [state, setState] = useState({ ...initialState, error: null });
const setErrorState = useCallback(stateUpdater => {
if (typeof stateUpdater === 'function') {
setState(state => {
try {
return {error: null, ...stateUpdater(state) };
} catch (error) {
return {...state, error };
}
});
} else {
setState({error: null, ...stateUpdater });
}
}, []);
return [state, setErrorState];
}
Another possibility is to move error handling to parent ponent (possibly with higher-order ponent) and catch errors there.
If I have a ponent that's similar to the below:
import React, { useState } from "react";
import ReactDOM from "react-dom";
function calculateNewValueAndMaybeError(test) {
try {
// do something with test that might error
// const newTest = functionThatMightError(...)
// if it doesn't error, return { value: newTest, error: '' }
} catch (err) {
// is it correct to now return this in case of error?
// return { value: test.value, error: 'Something went wrong' }
}
}
function App() {
const [test, setTest] = useState({ value: "", error: "" });
return (
<div>
<p>Test: {test.value}</p>
<p>Error: {test.error}</p>
<button onClick={() => setTest(calculateNewValueAndMaybeError(test))}>
Click me
</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
And I have a button that should mutate the state of my variable called test
, is there a way to handle errors without having to pass around the object with both value and error inside all the time?
Otherwise, doesn't this result in necessary code sometimes like the below:
onChange={() => setTest({ value: e.target.value, error: test.error })}
Where you want to update the value of test.value
but retain the value of test.error
.
I feel like there's something obvious that I'm missing.
Thanks!
If I have a ponent that's similar to the below:
import React, { useState } from "react";
import ReactDOM from "react-dom";
function calculateNewValueAndMaybeError(test) {
try {
// do something with test that might error
// const newTest = functionThatMightError(...)
// if it doesn't error, return { value: newTest, error: '' }
} catch (err) {
// is it correct to now return this in case of error?
// return { value: test.value, error: 'Something went wrong' }
}
}
function App() {
const [test, setTest] = useState({ value: "", error: "" });
return (
<div>
<p>Test: {test.value}</p>
<p>Error: {test.error}</p>
<button onClick={() => setTest(calculateNewValueAndMaybeError(test))}>
Click me
</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
And I have a button that should mutate the state of my variable called test
, is there a way to handle errors without having to pass around the object with both value and error inside all the time?
Otherwise, doesn't this result in necessary code sometimes like the below:
onChange={() => setTest({ value: e.target.value, error: test.error })}
Where you want to update the value of test.value
but retain the value of test.error
.
I feel like there's something obvious that I'm missing.
Thanks!
Share Improve this question asked Mar 7, 2019 at 20:19 SudoscienceSudoscience 3013 silver badges11 bronze badges1 Answer
Reset to default 3That there's a need to merge two states suggests that they should be either handled as different states:
function App() {
function calculateNewValueAndMaybeError(test) {
try {
...
setValue(...);
setError(null);
} catch (err) {
setError('Something went wrong');
}
}
const [error, setError] = useState(null);
const [value, setValue] = useState('');
return (
<div>
<p>Test: {value}</p>
<p>Error: {error}</p>
<button onClick={() => setTest(calculateNewValueAndMaybeError(test))}>
Click me
</button>
</div>
);
}
Or custom state hook could be used to merge error
field:
const useErrorState = initialState => {
const [state, setState] = useState({ ...initialState, error: null });
const setErrorState = useCallback(stateUpdater => {
if (typeof stateUpdater === 'function') {
setState(state => {
try {
return {error: null, ...stateUpdater(state) };
} catch (error) {
return {...state, error };
}
});
} else {
setState({error: null, ...stateUpdater });
}
}, []);
return [state, setErrorState];
}
Another possibility is to move error handling to parent ponent (possibly with higher-order ponent) and catch errors there.
本文标签: javascriptHandling errors using React39s useStateStack Overflow
版权声明:本文标题:javascript - Handling errors using React's useState - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745625863a2159856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论