javascript - 如何在功能组件中使用 {...this.props}

标签 javascript reactjs react-native

我在 Udemy 上购买了 React-Native 类(class),教授 React-Native 的人使用了 {...this.props}。 但这对我不起作用。这是错误消息。

TypeError: undefined is not an object(evaluating '_this.props')

我应该做什么?

登录表单.js

import React from 'react';
import { StyleSheet, ScrollView, View, Text, Image } from 'react-native';
import Input from '../components/Input';
const LoginForm: () => React$Node = () => {
  return (
    <>
      <View>
        <Text style={styles.signInText}>SIGN IN</Text>
        <Input 
          placeholder="Username"
        />
      </View>
    </>
  );
};

const styles = StyleSheet.create({
  signInText:{
    fontWeight:'bold',
    marginVertical:25,
    color:'#189BDD'
  }
});

export default LoginForm; 

输入.js

import React from 'react';
import { StyleSheet, ScrollView, View, TextInput, Image } from 'react-native';
const Input: () => React$Node = () => {
  return (
    <>
      <View>
        <TextInput
          {...this.props}
        />
      </View>
    </>
  );
};

const styles = StyleSheet.create({});

export default Input;

Udemy 老师 SS

最佳答案

this 不会存在于功能组件上。在功能组件中,您可以将 props 作为参数来访问,但您尚未传递该参数。

const Input = (props) => {

...

  <TextInput
    {...props}
  />
}

关于javascript - 如何在功能组件中使用 {...this.props},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60532328/

相关文章:

JavaScript - 在重定向之前记录

javascript - javascript中的javascript输入问题

reactjs - 使用 Git Gateway 配置 Netlify CMS 的问题

reactjs - 如何获取用户输入(YouTube视频链接)并使用React.js将其嵌入为iframe?

android - 当用户使用“主页”或“概述”按钮离开应用程序时,如何更新 React Native 中的组件?

components - 查看组件 `` 的配置 getter 回调必须是一个函数

javascript - 用javascript中的递增数字替换HTML文本

javascript - 删除更多需要的项目的方法

reactjs - 使用 Jest with Enzyme 的 Typescript 测试无法识别组件

react-native - React Art <Text/> 旋转