Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include Javascript code in the render function of a react native app? [duplicate]

Tags:

react-native

I'm a React and React native noob so its probably going to be a very silly question but how can I use the 'for loop' inside the render function to include my components? This is what I did

render() {
  return (
    <View style={styles.container}>
      { for (let i=0; i<20; i++) 
        {
           //This is my component
           <CounterButton />
        }
      }
    </View>
  );
}

but it threw an error, then someone suggested to include the code in a method and call it inside the render function so I did

createButtons() {
  for (let i =0; i<20; i++){
    <CounterButton />;
  }
}

render() {
  return (
    <View style={styles.container}>
      {this.createButtons()}

    </View>
  );
}

now I dont see errors but its just a blank screen. I know you can access props but Is the render function supposed to contain primarily only JSX code? Thanks in advance.

like image 706
Dipanshu Juneja Avatar asked Aug 25 '17 14:08

Dipanshu Juneja


People also ask

How do I run JavaScript code in React Native?

To run JavascriptReact Native uses JavaScriptCore (JavaScript engine in Safari) on Android/ iOS simulators and devices. In case of Android, React Native bundles the JavaScriptCore along with the application. In case of iOS, React Native uses the JavaScriptCore provided by the iOS platform.

Can we reuse react JS code in React Native?

This is possible because React Native under the hood utilizes the same component which is used on the web. Normally, projects fall into an instance when they should create a platform that is reusable in React JS, for both React Native mobile apps and the website.

Why is my functional component rendering twice?

The reason why this happens is an intentional feature of the React. StrictMode . It only happens in development mode and should help to find accidental side effects in the render phase.

Can I use JavaScript in React Native?

JavaScript Runtime​ When using React Native, you're going to be running your JavaScript code in two environments: In most cases, React Native will use JavaScriptCore, the JavaScript engine that powers Safari.


2 Answers

Jsx can contain expressions which return Components or arrays of components. In you case you need something that returns an array of components.

Sticking with the for loop:

createButtons() {
  let buttons = [];
  for (let i=0; i<20; i++){
    buttons.push(<CounterButton />);
  }
  return buttons;
}

If you wanted to do it in the jsx, something like this should work:

render() {
  return (
    <View style={styles.container}>
      {[...Array(20).keys()].map(<CounterButton />)}    
    </View>
  );
}
like image 198
WayneC Avatar answered Oct 12 '22 00:10

WayneC


Your createButtons function doesn't return anything. You could add the buttons to an array and return that.

createButtons() {
  const buttons = [];
  for (let i =0; i<20; i++){
    buttons.push(<CounterButton />);
  }
  return buttons;
}

Note that in react-native when rendering an array of elements, each element needs to have a key property.

like image 43
Kraylog Avatar answered Oct 12 '22 02:10

Kraylog