0

I want to create a function and have it return an html like in react (Without use react or jsx)

const func = () => {
  return (
    <h1>Hello World!</h1>
  );
}

1 Answer 1

1

Without use react or jsx

If you don't want to use JSX then clearly you can't use JSX. You can return a string:

const func = () => {
  return '<h1>Hello World!</h1>';
}

document.querySelector('#test').innerHTML = func();
<div id="test"></div>

Or perhaps an element:

const func = () => {
  const el = document.createElement('h1');
  el.innerText = 'Hello World!';
  return el;
}

document.querySelector('#test').appendChild(func());
<div id="test"></div>

Sign up to request clarification or add additional context in comments.

11 Comments

And without using quotes or createelement
@yepshushi: What do you mean? What exactly are you trying to do?
return an html in the function instead of a string
@yepshushi: And what do you consider to be "an html"? If you want to use JSX without using React, that may be possible. But in the question you explicitly state that you don't want to use JSX. Now you're also stating that you don't want to use strings. Or elements. Do you even want to use JavaScript at all? Are you even using a web browser? What are these random restrictions and what problem are you actually trying to solve? Please clarify the question instead of adding arbitrary requirements in comments.
sorry, what I'm trying to explain is that I wanted the component function to write as html ``return (<h1></h1>)``` and I was wondering if there was a way to do that without using jsx or babel, and I wanted it to return as a string when the function was executed, for example component() // => "<h1></h1>"
|

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.