【问题标题】:How to use react-native-mmkv in my react native app如何在我的 React Native 应用程序中使用 React-Native-mmkv
【发布时间】:2023-02-23 03:31:00
【问题描述】:

我正在尝试在我的 React Native 应用程序中使用 react-native-mmkv 包,任何人都可以帮助我做到这一点吗?我安装了这个包并按照文档上的一些步骤进行操作,但我无法正确设置它我正在使用最新版本的 rn cli 和 react-native-mmkv 我如何在我的项目中使用它我尝试做 MMKV.set('user ', JSON.stringify(数据));它不工作 它给我类型错误 如何初始化 mmkv 存储?

从'react-native-mmkv'导入{MMKV};

导出常量存储 = 新 MMKV()

const 登录 = ({ 导航 }) => {

const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [loading, setLoading] = useState(false)

const handleLogin = () => {
    if (email == '' || password == '') {
        alert('Please enter email and password')
    }
    else {
        setLoading(true)
        fetch('http://10.0.2.2:3000/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                email,
                password
            })
        })
            .then(res => res.json())
            .then(async data => {
                console.log(data)
                console.log(JSON.stringify(data))
                if (data.error) {
                    setLoading(false)
                    alert(data.error)
                }
                else if (data.message == 'Successfully Signed In') {
                    setLoading(false)
                    await MMKV.set('user', JSON.stringify(data));
                }
            })
            .catch(err => {
                setLoading(false)
                alert(err)
                console.log(err)
            })
    }
}

return (
   // ...
)

}

导出默认登录

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    当您启动商店时

    export const storage = new MMKV()
    

    你使用导出变量调用它,所以

    instead: MMKV.set()
    do: storage.set()
    
    

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 2021-07-21
      • 1970-01-01
      • 2021-02-08
      • 2018-10-08
      相关资源
      最近更新 更多