【问题标题】:AutoCorrect worked without being passed in as a prop?自动更正工作没有作为道具传递?
【发布时间】:2018-07-06 15:55:03
【问题描述】:

基本上我有一个组件的行为类似于TextInput,如下所示

//Input.js

import React from 'react';
import { Text, TextInput, View } from 'react-native';

const Input = ({ label, value, onChangeText }) => {
    const { inputStyle, containerStyle, labelStyle } = styles;

    return (
        <View style={containerStyle}>
            <Text style={labelStyle}>{ label }</Text>
            <TextInput
                style={inputStyle} 
                onChangeText={onChangeText}
                value={value}
            />
       </View>
    );
};

我还有另一个组件利用上面Input 组件的输入

//InputForm.js
import React, { Component } from 'react';
import { Button, Card, CardSection, Input } from './common';

class LoginForm extends Component {
    state = { text: '' };

    render() {
        return (
            <Card>
                <CardSection>
                    <Input 
                        autoCorrect={false}
                        value={this.state.text}
                        onChangeText={text => this.setState({ text })}
                        label='Email'
                    />
                </CardSection>
                <CardSection />
                <CardSection>
                    <Button>
                        Login
                    </Button>
                </CardSection>
            </Card>
        );
    }
}

export default LoginForm;

正如第一个 Input.js 中清楚显示的那样,我们只接收 label, value, onChangeText 作为道具,而在我的 InputForm.js 中,我传递了一个额外的道具 autoCorrect。现在发生的事情是,自动建议确实被禁用了。

如果我错了,请纠正我,Input 组件是一个自定义组件,它不应该理解 autoCorrect 的含义,因为我从未定义 Input.js 中的行为。那么为什么它会起作用呢?

【问题讨论】:

  • 也许我不明白这个问题,但默认情况下似乎禁用了自动建议。尝试设置 autoCorrect={true} (或者只是“autoCorrect”没有任何值,因为它是布尔值),看看会发生什么。
  • @DmitryBirin:在ios上,autoCorrect默认为true,因此只需将其设置为false就可以看到这里的效果。

标签: react-native components textinput prop


【解决方案1】:

自动更正的默认值为 true:https://facebook.github.io/react-native/docs/textinput.html#autocorrect

您不会在第二个示例中看到任何效果,因为您没有按照您正确的说法覆盖 TextInput 中的 autoCorrect 道具。

【讨论】:

  • 对不起,我知道autoCorrect 的默认值为true,这就是为什么在我的InputForm.js 中,我故意将autoCorrect 设置为false。我的意思是,通过将其设置为 false,自动更正确实被禁用。为什么?
  • 我不明白你的问题。您将自动更正设置为 false 并且您很惊讶它禁用了自动更正?这就是它应该发生的事情。
  • autoCorrect 设置为 Input 组件,其中 Input 组件是自定义组件。这就是为什么我在问题的第一部分包含Input.js。在Input 组件内的 下,没有任何设置处理或自动更正映射。现在你明白了吗?
  • 问题中只有一个例子,一个名为Input的自定义组件被另一个组件InputForm使用。
  • 啊,我明白了。我的猜测是您返回的功能组件实际上是一个组件类。 this.props.autoCorrect 应该是一样的。但你是对的,这实际上没有意义。您可以尝试调试 TextInput 以查看 props 在创建时的样子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
相关资源
最近更新 更多