クローンエンコーディングの復元2
18859 ワード
Map?
各
const friends = ["a", "b", "c"]
friends.map(current => {
console.log(current);
return 0;
})
=> frends = [0, 0, 0]another example
const foodILike = [
{
name : "a",
image : "a_i"
},
{
name : "b",
image : "b_i"
}
]
function Food({name, image}){
return <div>
<h2>I love {name}</h2>
<h2>This is image {image}</h2>
</div>
}
function App() {
return (
<div>
<h1>hello!</h1>
{foodILike.map(dish => (<Food name={dish.name} image={dish.image}/>))}
</div>
);
}
index.js:1 Warning: Each child in a list should have a unique "key"prop.=>keypropはFoodとして伝達できません.これは主にreact内部で使用するためです.
alt prop?
alt={name}
Check props
:私が受け取った道具が私の欲しい道具かどうかを確認します.
=>転送セクションで発生する可能性のあるエラーを確認する(複数のオプションがある)
rating : "sdf"
rating : PropTypes.number.isRequired
=> index.js:1 Warning: Failed prop type: Invalid prop rating
of type string
supplied to Food
, expected number
.//proptypeの名前は他に使えません.
render?
class App extends React.Component{
state = {
count : 0
};
render() {
return <div>The number is : {this.state.count}</div>;
}
}
State?
異なる点は、
なぜclasscomponentをインポートしますか?
=>stateはオブジェクトであり、componentのデータを格納するスペースがあり、データが変化するため
class App extends React.Component{
state = {
count : 0
};
...
}
function component vs class component
<button onClick={this.add()}
-> this.add():即時呼び出し
-> this.add:クリック時のみ呼び出す
ボタンをクリックするたびに自動的にレンダリング方法を実行
class App extends React.Component{
state = {
count : 0
};
add = () => {
this.state.count = 1;
};
minus = () => {
this.state.count = -1;
};
render() {
return (
<div>
<h1>The number is : {this.state.count}</h1>
<button onClick={this.add}>Add</button>
<button onClick={this.minus}>Minus</button>
</div>;
}
}
setStateを呼び出すと、reactはstateをリフレッシュしrender funcを呼び出します.class App extends React.Component{
state = {
count : 0
};
add = () => {
this.setState({ count : 1});
};
minus = () => {
this.setState({ count : -1});
};
render() {
return (
<div>
<h1>The number is : {this.state.count}</h1>
<button onClick={this.add}>Add</button>
<button onClick={this.minus}>Minus</button>
</div>;
}
}
this.setState({ count : this.state.count + 1});
いいコードじゃない this.setState(current => ({ count: current.count + 1}));
stateを設定する場合、reactionで外部状態に依存しない最適な方法Life cycle method
デフォルトではreactがcomponentを作成および削除する方法
mounting
次の方法は、構成部品インスタンスを作成し、DOMに挿入するときに、順番に呼び出されます.
updating
propsまたはstateが変更された場合、リフレッシュされます.次の方法は、構成部品の再レンダリング時に順番に呼び出されます.
unmounting
DOMから構成部品を削除すると、次の方法が呼び出されます.
Reference
この問題について(クローンエンコーディングの復元2), 我々は、より多くの情報をここで見つけました https://velog.io/@whtjdqls617/React-making-movie-service-2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol