小编Dav*_*han的帖子

React Native嵌套ScrollView锁定

我正在尝试在React Native中嵌套ScrollViews; 带有嵌套垂直滚动的水平滚动.

这是一个例子:

var Test = React.createClass({
    render: function() {
        return (
            <ScrollView
                style={{width:320, height:568}}
                horizontal={true}
                pagingEnabled={true}>

                {times(3, (i) => {
                    return (
                        <View style={{width:320, height:568}}>

                            <ScrollView>
                                {times(20, (j) => {
                                    return (
                                        <View style={{width:320, height:100, backgroundColor:randomColor()}}/>
                                    );
                                })}
                            </ScrollView>

                        </View>
                    );
                })}

            </ScrollView>
        );
    },
});

AppRegistry.registerComponent('MyApp', () => Test);
Run Code Online (Sandbox Code Playgroud)

外部卷轴可以完美地工作,但是当它在移动时触摸它时,内部卷轴会粘住.我的意思是:如果你滚动,抬起你的手指,当它仍然以动量移动时再次触摸它,它停止并且根本不会对触摸动作做出反应.要滚动更多,您必须抬起手指再次触摸.

这是如此可重复,感觉与手势响应器有关.

有没有人见过这个问题?

我怎么会开始调试这个?有没有办法看到什么响应触摸,授予和释放,什么时候?

谢谢.

更新:

看起来它是响应者系统,通过将onResponderMove侦听器放在内部和外部滚动器上:

<ScrollView 
    onResponderMove={()=>{console.log('outer responding');}}
    ...

    <ScrollView
        onResponderMove={()=>{console.log('inner responding');}}>
        ...
Run Code Online (Sandbox Code Playgroud)

很明显,外部ScrollView正在抓住控制权.我想,问题是如何在尝试垂直滚动时阻止外部滚动条控制?为什么只有在您尝试滚动已经移动的内部ScrollView时才会发生这种情况?

javascript react-native

31
推荐指数
5
解决办法
2万
查看次数

使用构造函数和state = {}在react组件中声明状态有什么区别?

我发现有两种方法可以在类组件中声明状态,如下所示

class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            name: 'John'
        }
    }

    render() {
        return  <div>{this.state.name}</div>
    }

}
Run Code Online (Sandbox Code Playgroud)

和

class App extends Component {
    state = {
       name: 'John'
    }

    render() {
        return  <div>{this.state.name}</div>
    }

}
Run Code Online (Sandbox Code Playgroud)

这两者有什么区别?

javascript ecmascript-6 reactjs

29
推荐指数
2
解决办法
3025
查看次数

如何检查组件是否已安装在React-Native ES6中

我正在我的应用程序中设置一个监听器,并在广播时使用强制更新,但它给出了错误forceUpdate无法在未安装的组件上调用.如何在isMounted()不推荐使用该功能的情况下检查是否已安装组件.

'use strict';
var React = require('react-native');
import ExpAndroid from './ExpAndroid';
var {
  AppRegistry,
  Image,
  ListView,
  TouchableHighlight,
  StyleSheet,
  Text,
  View,
  Component,
  AsyncStorage,
  Navigator,
  DeviceEventEmitter
} = React;

var rowID;
var img=require('./resource/ic_pause_white.png');





class Example1 extends Component{
  constructor(props) {
    super(props);
    this.state = {

    };
  }
  componentWillMount(){

      rowID = this.props.rowIdentity;
      console.log("rowID "+rowID);

  }


componentDidMount(){
  console.log('component  mounted')
  this.start();

  DeviceEventEmitter.addListener('playMusicStatus', (data)=> {


   if(data.playMusic==true){

     img=require('./resource/ic_pause_white.png');
       rowID++;
        this.setState(this.state);
        ExpAndroid.someMethod1("someurl);


  }



});
}

componentWillUnmount(){
  console.log('componentwill  unmounted')
}

  start() {

    var  url = "some url";
    ToastAndroid.prepareToPlay(url,true);
} …
Run Code Online (Sandbox Code Playgroud)

react-native

25
推荐指数
1
解决办法
3万
查看次数

图像'包含'resizeMode在反应原生中不起作用

我在真正的Android设备上使用React Native.在主应用程序组件上只使用以下渲染功能创建一个非常简单的应用程序时......

render() {
  <Image
    source={{uri:'http://resizing.flixster.com/DeLpPTAwX3O2LszOpeaMHjbzuAw=/53x77/dkpu1ddg7pbsk.cloudfront.net/movie/11/16/47/11164719_ori.jpg'}}
    style={
      {
        flex: 1,
        resizeMode: 'contain',
        backgroundColor: 'yellow'
      }          
    } />
}
Run Code Online (Sandbox Code Playgroud)

我在我的设备上得到以下结果: screenshot_2016-04-05-11-05-41

正如你所看到的那样整个背景是黄色的,这告诉我们图像元素确实占据了整个屏幕的大小.但这只是错误的.'cover'resizeMode确实按预期工作('stretch'模式也是如此).这是"包含"模式不起作用(从我的角度来看最重要的模式).将图像放在ListView上时问题变得更加严重,因为图像甚至都没有显示.

更新1 正如弗雷德里克所指出的,"包含"仅在图像大于容器大小时才有效.那么我们怎样才能让图像在保持纵横比的同时占据整个容器的大小?React中的样式尚不支持百分比,并且我不知道如何在加载图像后获取图像宽度和高度属性.与Image组件关联的任何事件都不提供该信息.

更新2 好消息.我现在正在使用React Native v0.24.1,现在看起来图像'包含'模式正如预期的那样工作,即使实际图像大小小于其容器.zvona的解决方案很好(尽管你需要记住,onLayout会给你渲染图像的图像视图大小,但不是正在加载的实际图像大小).至于现在,我不知道有什么方法可以找出实际的图像大小(让我们假设你是从网络资源中检索图像而你不知道大小,如果你想计算它可能非常重要它的宽高比).

react-native react-native-listview

24
推荐指数
5
解决办法
4万
查看次数

Flex React Native - 当内容到达边缘时,如何使用flex将内容分解到下一行

我有一个在我的样式容器中的图标列表,显示在一个flexDirection:'row'但是当图标数量超过视图宽度时,它们不会中断到下一行,但继续向右看不到视图.如果内容达到最大宽度,如何让内容中断到下一行?

造型:

var styles = StyleSheet.create({
  container: {
    width: SCREEN_WIDTH, //width of screen
    flexDirection:'row',
    backgroundColor: 'transparent',
    marginTop:40,
    paddingLeft:10,
    paddingRight:10,
    flex: 1,
  },
  iconText:{
    paddingLeft:10,
    paddingRight:10,
    paddingTop:10,
    paddingBottom:10
  },
});
Run Code Online (Sandbox Code Playgroud)

渲染:

<View style={styles.container}>

      <TouchableHighlight
        onPress={() => this.changeIcon(indexToChange, icons[0])}
        underlayColor='#F7F7F7'>
          <Text style={styles.iconText}><IonIcons name={icons[0]} size={30} color="#555" /></Text>
      </TouchableHighlight>

      <TouchableHighlight
        onPress={() => this.changeIcon(indexToChange, icons[1])}
        underlayColor='#F7F7F7'>
          <Text style={styles.iconText}><IonIcons name={icons[1]} size={30} color="#555" /></Text>
      </TouchableHighlight>

      <TouchableHighlight
        onPress={() => this.changeIcon(indexToChange, icons[2])}
        underlayColor='#F7F7F7'>
          <Text style={styles.iconText}><IonIcons name={icons[2]} size={30} color="#555" /></Text>
      </TouchableHighlight>

      ...//more continued on

</View>
Run Code Online (Sandbox Code Playgroud)

当图标向右移动宽度时,它们不会向下突破.这可能吗?

flexbox react-native

23
推荐指数
2
解决办法
2万
查看次数

在React-Native App中使用Axios GET和授权标头

我正在尝试使用axios来获取带有需要Authorization标头的API的GET请求.

我目前的代码:

const AuthStr = 'Bearer ' + USER_TOKEN;
Run Code Online (Sandbox Code Playgroud)

其中,USER_TOKEN被访问令牌需要.这个字符串连接可能是问题,好像我发布这个AuthStr = 'Bearer 41839y750138-391',以下GET请求工作并返回我之后的数据.

axios.get(URL, { 'headers': { 'Authorization': AuthStr } })
  .then((response => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error);
  });
Run Code Online (Sandbox Code Playgroud)

我也尝试将其设置为全局标题但没有成功.

fetch http-headers oauth-2.0 react-native axios

23
推荐指数
2
解决办法
6万
查看次数

如何在django迁移中执行原始SQL

我知道Django中的游标对象.在迁移中是否还有其他首选方法可以执行原始SQL?我想为我的一个模型表引入postgresql分区.分区逻辑是一系列功能和触发器,必须在我想要自动化的设置中添加到数据库中.

django postgresql database-partitioning django-migrations

22
推荐指数
1
解决办法
1万
查看次数

ReactJS失败并显示错误"无法读取未定义的属性'映射'"

在react.js教程之后,我遇到了一个错误:Uncaught TypeError: Cannot read property 'map' of undefined.

我正在遵循严格的教程,但坚持从服务器部分获取.当我使用url数据而不是硬编码的JSON数据提供commentBox时,会出现错误.

/**@jsx React.DOM*/

var converter = new Showdown.converter();

var data = [
  { Author: "Daniel Lo Nigro", Text: "Hello ReactJS.NET World!" },
  { Author: "Pete Hunt", Text: "This is one comment" },
  { Author: "Jordan Walke", Text: "This is *another* comment" }
];

var Comment = React.createClass({
  render: function() {
  var rawMarkup = converter.makeHtml(this.props.children.toString());
    return (
      <div className="comment">
        <h2 className="commentAuthor">
          {this.props.author}
        </h2>
        <span dangerouslySetInnerHTML={{__html: …
Run Code Online (Sandbox Code Playgroud)

reactjs

21
推荐指数
2
解决办法
2万
查看次数

在事件中反应本机播放声音

我有一个组件,如:

import React, { Component } from 'react'
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'


class MovieList extends Component {


    handlePress() {
        // Play some sound here
    }

    render() {
        const { movie } = this.props
        return (
            <TouchableOpacity onPress={this.handlePress.bind(this)}>
                <View style={styles.movie}>
                    <Text style={styles.name}>{movie.name}</Text>
                    <View style={styles.start}>
                        <Text style={styles.text}>Start</Text>
                    </View>
                </View>
            </TouchableOpacity>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里当我触摸时view我想播放一些声音.我搜索了它,但没有找到任何适当的答案

无论如何,当我按下某些东西时我能发出声音吗?我怎样才能做到这一点 ?

react-native

21
推荐指数
2
解决办法
2万
查看次数

如何使用Jest测试异步存储?

我正在用React Native构建一个应用程序.我想尽量减少与数据库通信的频率,因此我大量使用AsyncStorage.但是DB和AsyncStorage之间的转换存在很多错误.因此,我想通过对它运行自动化测试来确保AsyncStorage拥有我相信它所做的数据.令人惊讶的是,我还没有找到任何有关如何在线进行此操作的信息.我试图独自完成这项工作并没有成功.

使用Jest:

it("can read asyncstorage", () => {
return AsyncStorage.getItem('foo').then(foo => {
  expect(foo).not.toBe("");
});  });
Run Code Online (Sandbox Code Playgroud)

此方法失败并显示错误:

TypeError: RCTAsyncStorage.multiGet is not a function
Run Code Online (Sandbox Code Playgroud)

删除返回将使其立即运行而无需等待值并不正确地通过测试.

当我尝试使用await关键字测试它时,我遇到了完全相同的错误:

it('can read asyncstorage', async () => {
this.foo = "";
await AsyncStorage.getItem('foo').then(foo => {
    this.foo = foo;
});
expect(foo).not.toBe(""); });
Run Code Online (Sandbox Code Playgroud)

有关如何成功运行AsyncStorage中的值断言的任何建议?我宁愿继续使用Jest,但如果它只能通过一些备用测试库来完成,我会对此持开放态度.

asynchronous reactjs jestjs react-native asyncstorage

19
推荐指数
3
解决办法
8459
查看次数