Blog Day 40: React State, insertDash
9740 ワード
1. TIL (Today I Learned)
State
The "state" of the application
In the state & props video, it was said that state is a value that can be changed inside a component like Toggle Switch or Counter. What would be a "state"in a real application. Let's take a shopping mall shopping cart as an example. The user distinguishess between the items to be purchased and the items not to be purchased at the moment by checking the checkboxes. If we divide this into th status in the shopping cart, it is a checked status & unchecked status.
The implementation of the checkbox in code is as follows. In the example below, the text displayed simply depends on the checked status, but if this is applied to the shopping mall, the number of items to be purchased on the purchase amount will change depending on whether the check is made or not, and the user's screen will also change accordingly. As such, mutable values within a component, i.e. state, should be treated as React state.import React, { useState } from "react";
import "./style.css";
function CheckboxExmaple() {
const [isChecked, setIsChecked] = useState(false);
const handleChecked = (event) => {
setIsChecked(event.target.checked);
};
return (
<div className="App">
<input type="checkbox" checked={isChecked} onChange={handleChecked} />
<span>{isChecked ? "Checked!" : "Unchecked"}</span>
</div>
);
};
export default CheckboxExample;
State hook, useState
useState Usage
React provides a special function called useState as one way to handle state. Let's take a look at how this function is used and how it works using the checkbox example above.
To use useState, we need to call useState from React. Let's call useState with the import keyword.import { useState } from "react";
After that, useState is called inside the component. Calling useState is equivalent to declaring a variable called "state", which can be named anything. Normally, variables disappear when the function ends, but the state variable is not destroyed by React when the function ends.
From a syntatical point of view, isChecked and setIsChecked in the example below are variables assigned by destructuring the return value of useState.function CheckboxExample() {
const [isChecked, setIsChecked] = useState(false);
function CheckboxExample() {
const [isChecked, setIsChecked] = useState(false);
// is same as
const StateHookArray = useState(false);
const isChecked = StateHookArray[0];
const setIsChecked = StateHookArray[1];
}
Calling useState returns an array, where the first element of the array is the current state variable, and the 2nd element is a function that can update this variable. The value passed as an argument to useState is the initial value of state.const [state storage variable, state update function] = useState(state initial value);
Let's write the actual code.function CheckboxExample() {
const [isChecked, setIsChecked] = useState(false);
import React, { useState } from "react";
import "./style.css";
function CheckboxExmaple() {
const [isChecked, setIsChecked] = useState(false);
const handleChecked = (event) => {
setIsChecked(event.target.checked);
};
return (
<div className="App">
<input type="checkbox" checked={isChecked} onChange={handleChecked} />
<span>{isChecked ? "Checked!" : "Unchecked"}</span>
</div>
);
};
export default CheckboxExample;
import { useState } from "react";
function CheckboxExample() {
const [isChecked, setIsChecked] = useState(false);
function CheckboxExample() {
const [isChecked, setIsChecked] = useState(false);
// is same as
const StateHookArray = useState(false);
const isChecked = StateHookArray[0];
const setIsChecked = StateHookArray[1];
}
const [state storage variable, state update function] = useState(state initial value);
function CheckboxExample() {
const [isChecked, setIsChecked] = useState(false);
<span>{isChecked ? "Checked!!" : "Unchecked"}</span>
Update State
function Checkbox Example() {
const [isChecked, setIsChecked] = useState(false);
const handleChecked = (event) => {
setIsChecked(event.target.checked);
};
return (
<div className="App">
<input type="checkbox" checked={isChecked} onChange={handleChecked} />
<span> {isChecked ? "Checked!!" : "Unchecked"}</span>
</div>
);
};
Precautions
So far, we have learned how to use state hooks. Let's learn about the precautions when using the state hook.
insertDash
Take a string as input and return a string with '-' added between consecutive odd numbers.
function insertDash(str) {
let result = '';
for (i = 0; i < str.length; i++) {
if (str[i] % 2 !== 0 && str[i-1] % 2 !== 0) {
result = result + '-';
}
result = result + str[i];
}
return result;
}
2. 3 Things to be Thankful for
3. Ideas and Things to think about
Reference
この問題について(Blog Day 40: React State, insertDash), 我々は、より多くの情報をここで見つけました https://velog.io/@inseoparkk/Blog-Day-40-React-State-insertDashテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol