Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Reactのいいなと思ったところ
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Natsuki
January 15, 2025
770
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Reactのいいなと思ったところ
クラスメソッドのReact事情大公開スペシャル#6
https://classmethod.connpass.com/event/339885/presentation/
Natsuki
January 15, 2025
More Decks by Natsuki
See All by Natsuki
PHPStanのエラーをprettyにしようとしている
natsukiengr
0
32
jsが型安全になったっていい
natsukiengr
0
210
PHPStanをチームに内緒で開発に取り入れる方法
natsukiengr
1
1.8k
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
55
10k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Exploring anti-patterns in Rails
aemeredith
3
410
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
The untapped power of vector embeddings
frankvandijk
2
1.8k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
430
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
530
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.5k
So, you think you're a good person
axbom
PRO
2
2.1k
Transcript
Reactのいいなと思ったところ Natsuki
今日話すこと 普段の業務ではVueを使用していて、最近Reactも業務外で勉強し始めました 初心者の目線で「Reactのココいいな」と思った点を共有したいと思います 自己紹介 Natsuki 普段はLaravel + Vueで開発
宣伝 Reactを学ぶ上で、VSCodeの拡張機能を作ってみました。 Your Themes
demo https://github.com/natsuki-engr/your-themes/blob/a1ce518a99009fd5baf489e592958c3c35b5dfcf/img/demo.gif
本題 「Reactのいいなと思ったところ」
dangerouslySetInnerHtml タグを含んだ文字列をHTMLタグとしてレンダリングしたい場合の`setInnerHtml`と同じ XSS攻撃の危険性があるので、注意が必要 「危険なんだぞ」という強い警告を感じるところがいい vueだと v-html で同じ実装ができるが、罪悪感が無い const markup =
'<p>some raw html</p>'; return <div dangerouslySetInnerHTML={{__html: markup}} />; ` ` <script setup> const markup = '<p>some raw html</p>' </script>
一貫してJS 「JSを知っていれば大体書ける」 たとえばJSX
JSX JSXの構成はJSとタグなので、テンプレートのために必要な知識が少ない JSのパラダイム/設計を持ち込める if(items.length === 0) { return <p>empty...</p> }
function ItemList({ items: Item[] }) { return ( <ul> {items.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul> ); }
レンダリングの更新が分かりやすい 基本的に、引数やステートが変わって再レンダリングされるときに 「全て再計算される」と思っておいて、 ・特定の再計算をスキップしたければ useMemo 配列から値の検索とか ・特定の処理をスキップしたければ useEffect APIの呼び出しとか `
` ` `
useEffect/useMemo の悪い例 不要な useEffect 不要な useMemo https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state ` ` function
Form() { const [firstName, setFirstName] = useState('Taylor'); const [lastName, setLastName] = useState('Swift'); const [fullName, setFullName] = useState(''); useEffect(() => { setFullName(firstName + ' ' + lastName); }, [firstName, lastName]); return /** ... */ } ` ` function Form() { const [firstName, setFirstName] = useState("Taylor"); const [lastName, setLastName] = useState("Swift"); const fullName = useMemo( () => firstName + " " + lastName, [firstName, lastName] ); return; /** ... */ } const fullName = firstName + " " + lastName
なぜ今日この話をしようと思ったか
参考 【React】誤解される useMemo と 誤用される useState ―― 「A の変更に反応して B
の値が変わる」と考え るべきでない https://qiita.com/honey32/items/58e56e407d4d87e294a4 【React】useEffect の標準動作は「依存配列の中身が変わると実行」ではない https://qiita.com/honey32/items/62edf5165aced7d0c4bf useState + useEffect -> 再レンダリング中に毎回計算 (重ければ useMemo) https://scrapbox.io/honey32/useState_+_useEffect_->_再レンダリング中に毎回計算_(重ければ_useMemo) props または state に基づいて state を更新する - そのエフェクトは不要かも https://ja.react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state