Skip to content

Commit 13c2ea6

Browse files
authored
Merge pull request #125 from xsnippet/useMemo
[React useMemo] Add useMemo hook for syntaxes
2 parents db04da2 + c7a7f41 commit 13c2ea6

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/components/NewSnippet.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from 'react'
1+
import React, { useEffect, useRef, useMemo } from 'react'
22
import { connect } from 'react-redux'
33
import AceEditor from 'react-ace'
44
import { WithContext as Tags } from 'react-tag-input'
@@ -70,14 +70,14 @@ const NewSnippet = ({ dispatch, history, syntaxes }) => {
7070
const handleSyntax = syntax => ({ syntax })
7171
const handleContent = content => ({ content })
7272

73-
const getSyntaxes = () => {
73+
const memoizedSyntaxes = useMemo(() => {
7474
const { modesByName } = getModesByName()
7575

7676
return syntaxes.map(item => ({
7777
name: modesByName[item].caption,
7878
value: item,
7979
}))
80-
}
80+
}, [syntaxes])
8181

8282
const renderValidationError = () => (error && <Notification message={error} />)
8383

@@ -128,7 +128,7 @@ const NewSnippet = ({ dispatch, history, syntaxes }) => {
128128
</div>
129129
<div className="new-snippet-lang-wrapper">
130130
<ListBoxWithSearch
131-
items={getSyntaxes()}
131+
items={memoizedSyntaxes}
132132
onClick={(syntax) => handleChange(syntax, handleSyntax)}
133133
/>
134134
</div>

src/hooks/useForm.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useCallback } from 'react'
1+
import { useState, useEffect } from 'react'
22

33
const useForm = (cb, validate) => {
44
const [values, setValues] = useState({})
@@ -15,21 +15,21 @@ const useForm = (cb, validate) => {
1515
}
1616
}, [error, isSubmitting])
1717

18-
const handleSubmit = useCallback(e => {
18+
const handleSubmit = e => {
1919
e.preventDefault()
2020

2121
setError(validate(values))
2222
setIsSubmitting(true)
23-
})
23+
}
2424

25-
const handleChange = useCallback((e, setter) => {
25+
const handleChange = (e, setter) => {
2626
if (e.target) {
2727
e.persist()
2828
setValues(values => ({ ...values, [e.target.name]: e.target.value }))
2929
} else {
3030
setValues(values => ({ ...values, ...setter(e) }))
3131
}
32-
})
32+
}
3333

3434
return {
3535
handleChange,

0 commit comments

Comments
 (0)