美文网首页
react 计算优化

react 计算优化

作者: 考拉程序媛 | 来源:发表于2020-08-21 13:44 被阅读0次

    memoization
    import memoize from 'memoize-one';
    getFullName = memoize((firstName, lastName) => ${firstName} ${lastName});
    get fullName() {
    return this.getFullName(this.state.firstName, this.state.lastName);
    }
    ========================
    React Hooks
    import React, { useState, useMemo } from 'react';
    function Example(props) {
    [const] [firstName, setFirstName] = useState('');
    [const] [lastName, setLastName] = useState('');
    [const] renderFullName = useMemo(() => ${firstName} ${lastName}, [ firstName, lastName, ]);
    return <[div]>{renderFullName}</[div] }
    ======================
    $ npm install use-async-memo --save
    import {useAsyncMemo} from 'use-async-memo'

    const replyMessage = useAsyncMemo(async () => (
    // 这里的回调函数变成了async函数
    await api.fetchReply(message) // 这里也变成了await
    ), [message])

    相关文章

      网友评论

          本文标题:react 计算优化

          本文链接:https://www.haomeiwen.com/subject/iaxujktx.html