onClick()不会在React-Native App中触发功能

时间:2019-12-23 14:28:44

标签: javascript reactjs react-native

在我的本机移动应用程序中,我在row.js中编写了一个名为Row的组件,其中包含一个带有rog.cc: In instantiation of 'void append_impl(std::vector<T>&, Args&& ...) [with T = int; Args = {std::vector<int, std::allocator<int> >&}]': prog.cc:18:15: required from 'void append(std::vector<T>&, Args&& ...) [with T = int; Args = {std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >, int, int, int, int, int, std::__cxx11::list<int, std::allocator<int> >&}]' prog.cc:29:59: required from here prog.cc:10:3: error: static assertion failed 10 | static_assert((std::is_constructible_v<T, Args&&> && ...)); | ^~~~~~~~~~~~~ prog.cc:12:15: error: no matching function for call to 'std::vector<int>::push_back(std::vector<int>&)' 12 | (v.push_back(std::forward<Args>(args)), ...); 事件处理程序的TouchableOpacity。但是,单击该组件后,该功能将无法运行。

“行”组件显示有关特定电影的一些文本,单击时应运行handlePress()函数:

onClick()

在一个单独的app.js文件中,handlepress函数已被编写并作为道具传递给Row组件。 imdbID变量也从电影对象传递到组件:

const Row = props => (
  <TouchableOpacity onClick={() => props.handlePress(props.imdbID)} style={styles.row}>
    <Text>Some text</Text>
  </TouchableOpacity>
)
handlePress = imdbID => {
  // do something with imdbID
}

请有人可以告诉我我在做什么错以及为什么该功能无法运行。

5 个答案:

答案 0 :(得分:1)

如果您看看this guide,它没有onClick

您应该使用docs

const Row = props => (
  //          using onPress
  <TouchableOpacity onPress={() => props.handlePress(props.imdbID)} style={styles.row}>
    <Text>Some text</Text>
  </TouchableOpacity>
)

答案 1 :(得分:0)

React本机没有onClick的{​​{1}}。 请改用TouchableOpacity

答案 2 :(得分:0)

React-Native不提供onClick功能,而是提供onPress,因此将onClick替换为onPress

const Row = props => (
  <TouchableOpacity onPress={() => props.handlePress(props.imdbID)} style={styles.row}>
    <Text>Some text</Text>
  </TouchableOpacity>
)

希望这会有所帮助,毫无疑问

答案 3 :(得分:0)

使用onPress()代替onClick()

改进的代码

const Row = props => (
  <TouchableOpacity onPress={()=> props.handlePress(props.imdbID)} style={styles.row}>
    <Text>Some text</Text>
  </TouchableOpacity>
)

答案 4 :(得分:0)

使用onPress而不是onClick,并确保您要从本机而非手势处理程序导入可触摸

相关问题