Giter Site home page Giter Site logo

Comments (10)

sunnylqm avatar sunnylqm commented on June 2, 2024

from react-native-storage.

hainanzhang avatar hainanzhang commented on June 2, 2024

页面A (是获取数据)
代码:

getSignal = () => {
        storage.load({
            key: 'signal',
            autoSync: true,
            syncInBackground: true,
            syncParams: {
                extraFetchOptions: {
                    // 各种参数
                },
                someFlag: true,
            },
        }).then(ret => {
            console.log("信号源列表",ret)
            this.setState({
                SignalArr: ret
            }, () => {
                console.log("我state结束了", this.state.SignalArr)
            })
        }).catch(err => {
        });

    }

B页面(设置新的数据)

 storage.load({
                key: 'signal',
                autoSync: true,
                syncInBackground: true,
                syncParams: {
                    extraFetchOptions: {
                        // 各种参数
                        
                    },
                    someFlag: true,
                },
            }).then(ret => {
                arr =ret
                if( this.props.data){   //修改信号源
                    arr.splice(this.props.index,1,{
                        name:this.state.name,
                        type:this.state.type,
                        channel_num:this.state.channel_num,
                        id:this.randomId(1,10000000)
                    })
                }else{   //新增信号源
                    arr.push({
                        name:this.state.name,
                        type:this.state.type,
                        channel_num:this.state.channel_num,
                        id:this.randomId(1,10000000)
                    })
                }
                
                storage.save({
                    key: 'signal',
                    data: arr,
                    expires: null
                }).then( ( ) =>{
                    // this.props.addSignalModalHide()
                    

                    storage.load({
                        key: 'signal',
                        autoSync: true,
                        syncInBackground: true,
                        syncParams: {
                            extraFetchOptions: {
                                // 各种参数
                                
                            },
                            someFlag: true,
                        },
                    }).then(ret => {
                        console.log("设置信号源后的",ret)
                    })

                    
                    Actions.A()  //跳到 A页面
                })
            }).catch(err => {
                arr.push({
                    name:this.state.name,
                    type:this.state.type,
                    channel_num:this.state.channel_num,
                    id:this.randomId(1,10000000)
                })
                storage.save({
                    key: 'signal',
                    data: arr,
                    expires: null
                }).then( ( ) =>{
                    console.log("我设置了err")
                    this.props.addSignalModalHide()
                })
            });

image

from react-native-storage.

sunnylqm avatar sunnylqm commented on June 2, 2024

你这个嵌套太难读了,建议改成async/await再看看

from react-native-storage.

hainanzhang avatar hainanzhang commented on June 2, 2024

这是简化后的,在更新的页面可以获取到新的值,但是在前面已经获取过一次的,返回再次获取到的就不是新的值,还是老的值

image

A页面

storage.load({
            key: 'title',
            autoSync: true,
            syncInBackground: true,
            syncParams: {
                extraFetchOptions: {
                    // 各种参数
                },
                someFlag: true,
            },
        }).then(ret => {
            console.log("a页面获取的到的:",ret)
            this.setState({
                title: ret
            })

        }).catch(err => {  
         
        });

B页面:

 titleChange = (text)=>{

        storage.save({
            key: 'title',
            data: text,
            expires: null
        })
        this.setState({
            title:text
        })
        storage.load({
            key: 'title',
            autoSync: true,
            syncInBackground: true,
            syncParams: {
                extraFetchOptions: {
                    // 各种参数
                },
                someFlag: true,
            },
        }).then( (ret)=>{
            console.log( "B页面设置后的",ret)
        } ).catch( err =>{
        })

    }

from react-native-storage.

hainanzhang avatar hainanzhang commented on June 2, 2024

image

from react-native-storage.

sunnylqm avatar sunnylqm commented on June 2, 2024
  1. 建议用async/await语法
  2. 有没有可能new了多个storage的实例?
  3. 如果是B页面输入然后跳回A页面取值,那么没必要用storage传递,直接用回调更合适,更简单,性能也更好

from react-native-storage.

hainanzhang avatar hainanzhang commented on June 2, 2024

没有办法啊,数据只能存再本地,局域网软件的

from react-native-storage.

sunnylqm avatar sunnylqm commented on June 2, 2024

看你B页面的代码,不是textinput输入取值吗?这个跟本地局域网的有啥关系?

from react-native-storage.

hainanzhang avatar hainanzhang commented on June 2, 2024

需要textinput输入的值保留下来,下次打开app,直接读取这条数据

from react-native-storage.

hainanzhang avatar hainanzhang commented on June 2, 2024

谢谢你,想到解决方法了,可以在a页面设置就行了,不一定要在b页面设置

from react-native-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.