0

I have already created a web app and I need to convert it into a react-native-ios app. I have created a TextInput widget for the web app which does more than what the react-native TextInput componemt.

  1. How could I overwrite core functionality of TextInput such as the onchange method.

  2. How could I add extra functionality to the TextInput.

The web app's TextInput widget is written in JavaScript. I want to avoid objective-C, if at all possible.

Thanks.

3
  • I don't really understand how you would overwrite onChange, since it is merely a callback function which doesn't do anything. What is it you like to change or do? Commented Dec 20, 2018 at 12:31
  • changing the onChange is just one of many scenarios. I need to know can I overwrite or change the functionality of the parameters that TextInput takes. Thanks. Commented Dec 20, 2018 at 13:01
  • You shouldn't (and probably can't) overwrite core functionality. Wouldn't a simple component decorator work for you? Commented Dec 21, 2018 at 14:46

1 Answer 1

1

If I understand correctly, there is nothing react-native specific you would need to do here. You just use standard react techniques for adding functionality to existing components. So you can create a component which wraps TextInput, which allows you to pass thought any prop TextInput accepts. And you can just as well provide additional props to your component for other requirements.

import * as React from 'react';
import { 
  TextInput
} from 'react-native';

class CustomInput extends React.Component {

  constructor(props) {
    this.state = {text: 'default text'}
  }

  render(): JSX.Element {

    return (
      <TextInput
        {...this.props} // pass through props
        value={this.state.text}
        onChangeText={this.onChangeText} // update value as usual
        onChange={this.props.doSomethingSpecial} // call custom prop function for custom behaviour
      />
    ); 
  }

  onChangeText = (text) => {
    this.setState({text});
  }
}

export default CustomInput;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.