Categories
React Native Answers

How to align text input correctly in React Native?

Sometimes, we want to align text input correctly in React Native.

In this article, we’ll look at how to align text input correctly in React Native.

How to align text input correctly in React Native?

To align text input correctly in React Native, we set the textAlignVertical style to 'top'.

For instance, we write

<TextInput
  style={{
    flex: 1,
    width: "100%",
    height: 150,
    color: "#FFF",
    textAlignVertical: "top",
  }}
  multiline
  numberOfLines={10}
/>;

to set the textAlignVertical style of the TextInput component to 'top' so that it takes input from the top left corner.

We make the TextInput take multiple lines of input with the multiline prop.

And we set the numberOfLines prop to make the input accept 10 lines of input.

Conclusion

To align text input correctly in React Native, we set the textAlignVertical style to 'top'.

Categories
React Answers

How to import image (.svg, .png) in a React component?

Sometimes, we want to import image (.svg, .png) in a React component.

In this article, we’ll look at how to import image (.svg, .png) in a React component.

How to import image (.svg, .png) in a React component?

To import image (.svg, .png) in a React component, we can import the image directly.

For instance, we write

import React from "react";
import image from "./img1.png";
import "./helloWorld.scss";

const HelloWorld = () => (
  <>
    <img src={image} alt="some image" />
  </>
);
export default HelloWorld;

to add the img element into the HelloWorld component.

We set its src prop to the image that we import with the import statement.

We can import it with import since the Webpack file loader plugin is included with Create React app projects.

Conclusion

To import image (.svg, .png) in a React component, we can import the image directly.

Categories
React Answers

How to return multiple lines JSX in another return statement in React?

Sometimes, we want to return multiple lines JSX in another return statement in React.

In this article, we’ll look at how to return multiple lines JSX in another return statement in React.

How to return multiple lines JSX in another return statement in React?

To return multiple lines JSX in another return statement in React, we can wrap our components with fragments.

For instance, we write

const Comp = () => {
  return [1, 2, 3].map((n, index) => {
    return (
      <React.Fragment key={index}>
        <h3>Item {n}</h3>
        <p>Description {n}</p>
      </React.Fragment>
    );
  });
};

to wrap the h3 and p elements in the React.Fragement component in the map callback.

Then these elements will be rendered without a wrapper element around them.

Conclusion

To return multiple lines JSX in another return statement in React, we can wrap our components with fragments.

Categories
React Native Answers

How to add absolute position and in a flexbox container in React Native?

Sometimes, we want to add absolute position and in a flexbox container in React Native.

In this article, we’ll look at how to add absolute position and in a flexbox container in React Native.

How to add absolute position and in a flexbox container in React Native?

To add absolute position and in a flexbox container in React Native, we can set position to 'absolute' and set the left, top, right, and bottom values.

For instance, we write

const styles = StyleSheet.create({
  container: {
    position: "absolute",
    left: 0,
    top: 0,
    right: 0,
    bottom: 0,
  },
});

to set the position to 'absolute'.

And then we set the left, top, right, and bottom values to position the component with the styles in pixels.

Conclusion

To add absolute position and in a flexbox container in React Native, we can set position to 'absolute' and set the left, top, right, and bottom values.

Categories
React Answers

How to define more than one style name using CSS modules with React?

Sometimes, we want to define more than one style name using CSS modules with React.

In this article, we’ll look at how to define more than one style name using CSS modules with React.

How to define more than one style name using CSS modules with React?

To define more than one style name using CSS modules with React, we can put them class names in one string.

For instance, we write

function Footer(props) {
  return (
    <div className={styles.footer}>
      <div className={`${styles.description} ${styles.yellow}`}>
        <p>this site was created by me</p>
      </div>
    </div>
  );
}

to set the className prop of the inner prop to

`${styles.description} ${styles.yellow}`

to apply multiple classes imported from the CSS module.

Conclusion

To define more than one style name using CSS modules with React, we can put them class names in one string.