I want to make a typing efect, but the 2nd letter is not added.
For example:
I want to write "Hello World" and it displays "Hllo World".
my code:
export default function Error({ errorMsg }) {
const [errorString, setErrorString] = useState("");
let i = 0;
useEffect(() => {
writeError();
}, []);
function writeError() {
let timer = Math.floor(Math.random() * (100 - 25 + 1)) + 25;
setTimeout(() => {
if (i < errorMsg.length) {
setErrorString(errorString => ([
...errorString,
errorMsg.charAt(i)
]));
i++;
return writeError();
}
}, timer);
}
return (
<div>
<div id="wrp">
<div>{errorString}</div>
</div>
</div>
);
}
Why does the error only occur with the 2nd character, no matter how long the string is?
Edit
Thanks for the solutions, but the solution of @DecPK is the best in my opinion
fixed code:
export default function Error({ errorMsg }) {
const [errorString, setErrorString] = useState("");
let i = 0;
useEffect(() => {
writeError();
}, []);
function writeError() {
let timer = Math.floor(Math.random() * (100 - 25 + 1)) + 25;
setTimeout(() => {
if (i < errorMsg.length) {
setErrorString(errorString => ([
...errorString,
errorMsg.charAt(i++) // <-- Answer from @DecPK
]));
return writeError();
}
}, timer);
}
return (
<div>
<div id="wrp">
<div>{errorString}</div>
</div>
</div>
);
}
4
Answers
Answer from @DecPK
This is a React functional component that renders an error message with a typing effect. Here is a commented version of the code:
In summary, this component uses the useState and useEffect hooks to update the error message with a typing effect, using the setTimeout function to wait for a random timeout between each character.
The issue with updating a string in useState in React is that you cannot update a string directly like you can with other types such as numbers or booleans. Strings are immutable, meaning that once they are created, they cannot be modified. Therefore, any updates to a string variable create a new string and assign it to the variable.
To update a string in useState, you can use the set function provided by the useState hook, passing in the new string value as an argument. For example:
In the example above, the setMyString function updates the value of the myString state variable to ‘Hello React’. When using setMyString to update the string value, you should make sure to always pass a new string as the argument, even if it has the same value as the previous string. This is because React will only re-render the component if the state value reference changes.
Here’s an example of updating a string value using setMyString in response to a button click:
In this example, clicking the "Update string" button will update the myString state variable to ‘Hello React’ and cause the component to re-render with the new string value.
My Answer:
Please try to execute this.
Please consider the solution below. This component is responsible for managing it’s
cursor
state. It then calculates the text to show in render using theerrorMsg
prop. This is the React way.Sandbox demo available