Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - how to trim a string

I want to delete the whitespaces from the beginning and the end of a string. For example, given a string " Test ", I would like to receive "Test". I have tried JavaScript's methods and also some npm libraries, but they don't seem to work with React Native? Any thoughts?

enter image description here

like image 903
Monika Avatar asked Dec 09 '16 14:12

Monika


People also ask

How do I cut text in react native?

React Native allows you to automatically truncate text that will not fit within its <View> container. In most cases this is enough for the device to truncate the text, automatically adding ellipsis to the end of the string (…) after however many lines of text you have specified (In this case, we only want 1 line).

How do you cut a string in react?

You can also split() the string to array and then find the indexOf() the work, then join() using > to get the final result.

How do I remove a space from a string in react native?

To remove the empty spaces from the string, use the replace() method in JavaScript. This method searches a string for a specified value and returns a new string replacing those values. It's commonly used to search a string for a regular expression and replace that expression with the desired substring.

What is trim () in react?

The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.)


1 Answers

The problem is in your setEmail call and the ES6 syntax that you are using. When you do:

email => this.setEmail({email})

The transpiler transforms it into the following:

email => this.setEmail({email: email})

Which of course is an object.

Then, inside the function, you are trying to apply the trim function to an object, which of course results in failure. Try this instead:

email => this.setEmail(email)

You can read more on the ES6 syntax for object literals here.

like image 60
martinarroyo Avatar answered Oct 17 '22 18:10

martinarroyo