React idiomatic controlled input (useCallback, props and scope)

I was building a good old read-fetch-suggest lookup bar when I found out that my input lost focus on each keypress.

I learnt that because my input component was defined inside the header component enclosing it, changes to the state variable triggered a re-render of the parent which in turn redefined the input component, which caused that behaviour. useCallback was to be used to avoid this.

Now that I did, the state value remains an empty string, even though it’s callback gets called (as I see the keystrokes with console.log)

This gets fixed by passing the state and state setter to the input component as props. But I don’t quite understand why. I’m guessing that the state and setter which get enclosed in the useCallback get “disconnected” from the ones yield by subsequent calls.

I would thankfully read an explanation clearing this out. Why does it work one way and not the other? How is enclosing scope trated when using useCallback?

Here’s the code.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export const Header = () => {
const [theQuery, setTheQuery] = useState("");
const [results, setResults] = useState<ResultType>();
// Query
const runQuery = async () => {
const r = await fetch(`/something`);
if (r.ok) {
setResults(await r.json());
}
}
const debouncedQuery = debounce(runQuery, 500);
useEffect(() => {
if (theQuery.length > 3) {
debouncedQuery()
}
}, [theQuery]);
const SearchResults = ({ results }: { results: ResultType }) => (
<div id="results">{results.map(r => (
<>
<h4><a href={`/linkto/${r.id}`}>{r.title}</a></h4>
{r.matches.map(text => (
<p>{text}</p>
))}
</>
))}</div>
)
// HERE
// Why does this work when state and setter go as
// props (commented out) but not when they're in scope?
const Lookup = useCallback((/* {theQuery, setTheQuery} : any */) => {
return (
<div id='lookup_area'>
<input id="theQuery" value={theQuery}
placeholder={'Search...'}
onChange={(e) => {
setTheQuery(e.target.value);
console.log(theQuery);
}}
type="text" />
</div>
)
}, [])
return (
<>
<header className={`${results ? 'has_results' : ''}`}>
<Lookup /* theQuery={theQuery} setTheQuery={setTheQuery} */ />
</header>
{results && <SearchResults results={results} />}
</>
)
}
</code>
<code>export const Header = () => { const [theQuery, setTheQuery] = useState(""); const [results, setResults] = useState<ResultType>(); // Query const runQuery = async () => { const r = await fetch(`/something`); if (r.ok) { setResults(await r.json()); } } const debouncedQuery = debounce(runQuery, 500); useEffect(() => { if (theQuery.length > 3) { debouncedQuery() } }, [theQuery]); const SearchResults = ({ results }: { results: ResultType }) => ( <div id="results">{results.map(r => ( <> <h4><a href={`/linkto/${r.id}`}>{r.title}</a></h4> {r.matches.map(text => ( <p>{text}</p> ))} </> ))}</div> ) // HERE // Why does this work when state and setter go as // props (commented out) but not when they're in scope? const Lookup = useCallback((/* {theQuery, setTheQuery} : any */) => { return ( <div id='lookup_area'> <input id="theQuery" value={theQuery} placeholder={'Search...'} onChange={(e) => { setTheQuery(e.target.value); console.log(theQuery); }} type="text" /> </div> ) }, []) return ( <> <header className={`${results ? 'has_results' : ''}`}> <Lookup /* theQuery={theQuery} setTheQuery={setTheQuery} */ /> </header> {results && <SearchResults results={results} />} </> ) } </code>
export const Header = () => {

    const [theQuery, setTheQuery] = useState("");
    const [results, setResults] = useState<ResultType>();

    // Query

    const runQuery = async () => {
        const r = await fetch(`/something`);
        if (r.ok) {
            setResults(await r.json());
        }
    }

    const debouncedQuery = debounce(runQuery, 500);

    useEffect(() => {
        if (theQuery.length > 3) {
            debouncedQuery()
        }
    }, [theQuery]);



    const SearchResults = ({ results }: { results: ResultType }) => (
        <div id="results">{results.map(r => (
            <>
                <h4><a href={`/linkto/${r.id}`}>{r.title}</a></h4>
                {r.matches.map(text => (
                    <p>{text}</p>
                ))}
            </>
        ))}</div>
    )

    // HERE
    // Why does this work when state and setter go as 
    // props (commented out) but not when they're in scope?

    const Lookup = useCallback((/* {theQuery, setTheQuery} : any */) => {

        return (
            <div id='lookup_area'>

                <input id="theQuery" value={theQuery}
                    placeholder={'Search...'}
                    onChange={(e) => {
                        setTheQuery(e.target.value);
                        console.log(theQuery);
                    }}
                    type="text" />
            </div>
        )
    }, [])

    return (
        <>
            <header className={`${results ? 'has_results' : ''}`}>

                <Lookup /* theQuery={theQuery} setTheQuery={setTheQuery} */ />

            </header>

            {results && <SearchResults results={results} />}
        </>
    )
}

1

It’s generally not a good idea to have what are essentially component definitions inside of another component’s render function as you get all the challenges you have expressed. But I’ll come back to this and answer your original question.

When you do the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> const Lookup = useCallback((/* {theQuery, setTheQuery} : any */) => {
return (
<div id='lookup_area'>
<input id="theQuery" value={theQuery}
placeholder={'Search...'}
onChange={(e) => {
setTheQuery(e.target.value);
console.log(theQuery);
}}
type="text" />
</div>
)
}, [])
</code>
<code> const Lookup = useCallback((/* {theQuery, setTheQuery} : any */) => { return ( <div id='lookup_area'> <input id="theQuery" value={theQuery} placeholder={'Search...'} onChange={(e) => { setTheQuery(e.target.value); console.log(theQuery); }} type="text" /> </div> ) }, []) </code>
    const Lookup = useCallback((/* {theQuery, setTheQuery} : any */) => {

        return (
            <div id='lookup_area'>

                <input id="theQuery" value={theQuery}
                    placeholder={'Search...'}
                    onChange={(e) => {
                        setTheQuery(e.target.value);
                        console.log(theQuery);
                    }}
                    type="text" />
            </div>
        )
    }, [])

You are basically saying that when the Header component mounts, store a function that returns the JSX inside of Lookup, and also put this in a cache on mount of the component and never refresh it on subsequent renders — this is called memorization.

What defines that it’s only on the mount is the deps array []. The deps array is a list of things, that when changed between renders, will trigger the callback to be redefined, which at the same time will pull in a new scope of the component its enclosed within from that current render. Since you have not listed everything used inside of the callback function from the parent scope inside of the deps array, you are actually working around a bug that would be flagged if you used the proper lifting rules. It’s a bug because if something used inside like theQuery and setTheQuery changes, then the callback will not refresh — it will use the one stored in the local cache on mount which is referencing stale copies of those values. This is a very common source of bugs.

That said since you’ve done this, Lookup remains stable and won’t be refreshed. As you said, if it does refresh, you are going to see it gets remounted and things like its implicit DOM state (focus) are lost. In react, component definitions need to be referentially stable. Your useCallbacks are basically component definitions, but inside of another component render, and so are being recreated. I’ll come back to how you work around this properly shortly.

When you add theQuery={theQuery} setTheQuery={setTheQuery} you are working around the actual root problem by passing this data through to the callback from the parent scope so that it does not need to use the stale ones it has at hand from the initial render.

But what you have done is essentially write a component inside of another component, and that makes things much less encapsulated and gives rise to the problems you are seeing. You just need to simply define Lookup as its own component. And also, SearchResults.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
const Lookup = ({theQuery, setTheQuery}: any)) => {
return (
<div id='lookup_area'>
<input id="theQuery" value={theQuery}
placeholder={'Search...'}
onChange={(e) => {
setTheQuery(e.target.value);
console.log(theQuery);
}}
type="text" />
</div>
)
}
const SearchResults = ({ results }: { results: ResultType }) => (
<div id="results">{results.map(r => (
<>
<h4><a href={`/linkto/${r.id}`}>{r.title}</a></h4>
{r.matches.map(text => (
<p>{text}</p>
))}
</>
))}</div>
)
export const Header = () => {
const [theQuery, setTheQuery] = useState("");
const [results, setResults] = useState<ResultType>();
// Query
const runQuery = async () => {
const r = await fetch(`/something`);
if (r.ok) {
setResults(await r.json());
}
}
const debouncedQuery = debounce(runQuery, 500);
useEffect(() => {
if (theQuery.length > 3) {
debouncedQuery()
}
}, [theQuery]);
return (
<>
<header className={`${results ? 'has_results' : ''}`}>
<Lookup theQuery={theQuery} setTheQuery={setTheQuery} />
</header>
{results && <SearchResults results={results} />}
</>
)
}
</code>
<code> const Lookup = ({theQuery, setTheQuery}: any)) => { return ( <div id='lookup_area'> <input id="theQuery" value={theQuery} placeholder={'Search...'} onChange={(e) => { setTheQuery(e.target.value); console.log(theQuery); }} type="text" /> </div> ) } const SearchResults = ({ results }: { results: ResultType }) => ( <div id="results">{results.map(r => ( <> <h4><a href={`/linkto/${r.id}`}>{r.title}</a></h4> {r.matches.map(text => ( <p>{text}</p> ))} </> ))}</div> ) export const Header = () => { const [theQuery, setTheQuery] = useState(""); const [results, setResults] = useState<ResultType>(); // Query const runQuery = async () => { const r = await fetch(`/something`); if (r.ok) { setResults(await r.json()); } } const debouncedQuery = debounce(runQuery, 500); useEffect(() => { if (theQuery.length > 3) { debouncedQuery() } }, [theQuery]); return ( <> <header className={`${results ? 'has_results' : ''}`}> <Lookup theQuery={theQuery} setTheQuery={setTheQuery} /> </header> {results && <SearchResults results={results} />} </> ) } </code>

const Lookup = ({theQuery, setTheQuery}: any)) => {
        return (
            <div id='lookup_area'>

                <input id="theQuery" value={theQuery}
                    placeholder={'Search...'}
                    onChange={(e) => {
                        setTheQuery(e.target.value);
                        console.log(theQuery);
                    }}
                    type="text" />
            </div>
        )
}

const SearchResults = ({ results }: { results: ResultType }) => (
    <div id="results">{results.map(r => (
        <>
            <h4><a href={`/linkto/${r.id}`}>{r.title}</a></h4>
            {r.matches.map(text => (
                <p>{text}</p>
            ))}
        </>
    ))}</div>
 )


export const Header = () => {

    const [theQuery, setTheQuery] = useState("");
    const [results, setResults] = useState<ResultType>();

    // Query

    const runQuery = async () => {
        const r = await fetch(`/something`);
        if (r.ok) {
            setResults(await r.json());
        }
    }

    const debouncedQuery = debounce(runQuery, 500);

    useEffect(() => {
        if (theQuery.length > 3) {
            debouncedQuery()
        }
    }, [theQuery]);


    return (
        <>
            <header className={`${results ? 'has_results' : ''}`}>

                <Lookup theQuery={theQuery} setTheQuery={setTheQuery} />

            </header>

            {results && <SearchResults results={results} />}
        </>
    )
}

Since the components are defined outside of render, they are by definition defined once and they can’t access the scope of the Header component directly without you passing it to it via props. This is a correctly encapsulated and parameterized component that removes the opportunity to get into a scoping and memoization mess.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật