5

I have a typical website created with HTML, CSS, Javascript and I'm trying to convert it into react.

I can convert my HTML into JSX pretty easily with an online converter and my CSS is the same, I just have to import it differently.

But now I'm confused about how to link up my javascript files. Because my HTML is now JSX which is in a Javascript file as well.

Normally in html this is all I need to do to link my java script and everything works:

<script type="text/javascript" src="js/main.js"></script>

How would I do this in react such that my JSX will have the same functionality as did my HTML?

Right now whether I try to import it from file location:

import  './javascript/main.js'

It doesn't do anything. I'm not getting any errors. My JSX and CSS works fine and all I have for my CSS is (this import works fine):

import  './css/main.css'

If it should work, please let me know, it must mean there's an error elsewhere that I have to sort out.

Thanks in advance

Nilesh Kant
  • 351
  • 2
  • 10
i am bad at coding
  • 555
  • 1
  • 3
  • 14

2 Answers2

0

I don't think you can import js code on a react app, probably you have to create react-app first, then create its components and add you javascript code within these components, it´s pretty easy, i recomment you to read the documentation of react and see how it works. Hope it helped you.

0

You can include JavaScript functions inside JSX itself :

import React from 'react';

const Something=()=>{

  //  Your javascript functions :

  return(
    <div>
      Your Html here
    </div>
  )
}

export default Something

//  Or if You are using Class Component :

import React, { Component } from 'react';

class Something extends Component {
  state = {  }

  // Your Javascript Functions
  
  render() { 
    return ( 
      <div>
        Your HTML goes here 
      </div>
     );
  }
}
 
export default Something;

and if you want to import js file from another location you have to include export in your function:

export const Name=()=> {
             
}

and import:

import {Name} from '/location';
Nitesh
  • 11
  • 2