最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • react-native 入门指南《5》

    正文概述 掘金(快乐丁工人)   2020-12-22   660

    只会一种状态管理怎么行呢?

    mobx否?

    如果说redux是将所有的store集合在一起的纯函数操作,那么mobx则是分割store的可观察对象。

    说起这个可观察对象,那可就必须要提提js业界著名的rxjs了。

    RXjs以其著名的C型学习曲线闻名遐迩。

    react-native 入门指南《5》

    真是一宿一杜之敌啊!挞靼开!!!

    呵呵,一杜?小伙子身体不行。

    师傅停车!

    站牌

    先来三连

    yarn add mobx

    yarn add react-mobx

    yarn ios

    import React from 'react';
    import {Text} from 'react-native';
    import {observer} from 'mobx-react';
    @observer
    const Login = () => {
      return (
        <>
          <Text>123</Text>
        </>
      );
    };
    export default Login;
    

    0.SyntaxError: Support for the experimental syntax 'decorators-legacy' isn't currently enabled

    不支持装饰器

    新建 .babelrc

    {
      "plugins": [
        [
          "@babel/plugin-proposal-decorators",
          {
            "legacy": true
          }
        ]
      ]
    }
    

    1.SyntaxError: Leading decorators must be attached to a class declaration

    路走窄了呀。

    import React from 'react';
    import {Text} from 'react-native';
    import {observer} from 'mobx-react';
    @observer
    class Login extends React.Component<any, any> {
      render() {
        return (
          <>
            <Text>123</Text>
          </>
        );
      }
    }
    export default Login;
    

    3.Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.

    tsconfig.json

    "experimentalDecorators": true
    

    login/index.tsx

    import React from 'react';
    import {Alert, Button, Text, TextInput, View} from 'react-native';
    import {observer} from 'mobx-react';
    import {autorun, observable} from 'mobx';
    import {loginStore} from './store';
    @observer
    class Login extends React.Component<any, any> {
      @observable employee = '';
      @observable salary = '';
      componentDidMount() {
        loginStore.searchHandle();
      }
      render() {
        return (
          <View style={{marginTop: 46}}>
            <Text>{loginStore.str}</Text>
            <View style={{flexDirection: 'row', marginTop: 12}}>
              <Text>总工资:</Text>
              <Text>{loginStore.totalSalary}</Text>
            </View>
            <Text>{loginStore.str}</Text>
            {loginStore.list.map((item, key) => {
              return (
                <View
                  style={{flexDirection: 'row', marginTop: 12}}
                  key={item.employee + '' + key}> 
                 <Text>
                    {item.employee} --- {item.salary}
                  </Text>
                  <Button
                    title={'开除'}
                    onPress={() => {
                      loginStore.list.splice(key, 1);
                    }}
                  />
                </View>
              );
            })}
            <View
              style={{
                flexDirection: 'row',
                flexWrap: 'nowrap',
                marginTop: 12,
              }}>
              <View
                style={{
                  width: '50%',
                }}>
                <View
                  style={{
                    flexDirection: 'row',
                    alignItems: 'center',
                  }}>
                  <Text>姓名: </Text>
                  <TextInput
                    value={this.employee}
                    onChangeText={(employee) => {
                      this.employee = employee;
                    }}
                    style={{
                      flex: 1,
                      borderWidth: 1,
                      borderColor: '#ccc',
                      height: 30,
                    }}
                  />
                </View>
              </View>
            </View>
            <View
              style={{
                flexDirection: 'row',
                flexWrap: 'nowrap',
                marginTop: 12,
              }}>
              <View
                style={{
                  width: '50%',
                }}>
                <View style={{flexDirection: 'row', alignItems: 'center'}}>
                  <Text>薪水: </Text>
                  <TextInput
                    value={this.salary}
                    onChangeText={(salary) => {
                      this.salary = salary;
                    }}
                    style={{
                      flex: 1,
                      borderWidth: 1,
                      borderColor: '#ccc',
                      height: 30,
                    }}
                  />
                </View>
              </View>
              <View style={{width: '15%'}}>
                <Button
                  title={'添加'}
                  onPress={() => {
                    const {salary, employee} = this;
                    autorun(
                      () => {
                        if (!this.employee) {
                          Alert.alert('提示', '员工姓名不能为空!', [
                            {text: 'OK', onPress: () => console.log('OK Pressed')},
                          ]);
                          return;
                        }
                        if (!this.salary) {
                          Alert.alert('提示', '员工工资!', [
                            {text: 'OK', onPress: () => console.log('OK Pressed')},
                          ]);
                          return;
                        }
                        loginStore.list.push({
                          employee,
                          salary,
                        });
                        this.employee = '';
                        this.salary = '';
                      },
                      {delay: 300},
                    );
                  }}
                />
              </View>
            </View>
          </View>
        );
      }}
    export default Login;
    

    login/store/index.ts

    import {action, configure, observable} from 'mobx';
    configure({enforceActions: true} as any);
    interface IState {
      str: string;
      list: {employee: string; salary: string}[];
      [key: string]: any;
    }
    const state: IState = {
      str: '打工人管理系统',
      list: [],
      searchHandle: action(function reset() {
        new Promise((resolve) => {
          setTimeout(() => {
            resolve([{employee: '王某', salary: '1000'}]);
          }, 3000);
        }).then((res) => {
          loginStore.list = res as [];
        });
      }),
      get totalSalary() {
        return this.list.reduce((v, o) => {
          return v + parseInt(o.salary, 10);
        }, 0);
      },
    };
    const loginStore = observable(state);
    export {loginStore};
    

    4.TextInput更新不同步

    @abservable 更新时数据流不同步导致。

    of(employee)
      .pipe(delay(1000))
      .subscribe((txt) => {
        this.employee = txt;  }
    );
    

    视图只更新一次……

    ten years later

    react-native 入门指南《5》

    算了,算了,好好的路不走偏偏要翻墙。

    state = {
      employee: '',
      salary: '',
    };
    

    其他部分自己改……

    react-native 入门指南《5》

    统统开除掉。

    告辞!


    起源地下载网 » react-native 入门指南《5》

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元