1

Let's say that I have a function called Foo() which outputs "Hello world!", and I want to make a function called Bar() which takes the output of Foo() and bolds it. Desired output:

Hello world!

Things I have tried:

function Bar() {
    return <b>Foo()</b>;
}

Output: Foo()

function Bar() {
    var f = <b>Foo()</b>;
    return f;
}

Output: Foo()

function Bar() {
    var f = Foo();
    return <b>f</b>;
}

Output: f

function Bar() {
    return "<b>" + Foo() + "</b>";
}

Output: <b>[object Object]</b>

What do I need to do to get Hello world! as the output?

3 Answers 3

2

If you want to include a string in JSX, you'll have to add it to a curly braces parentheses. Here is an example:

function Bar() {
  return <b>{ Foo() }</b>;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I figured out how to do it, though it's not obvious to someone coming from C# and C++.

You need to surround all code declared between HTML tags with curly braces, like this:

function Bar() {
    return <b>{Foo()}</b>;
}

The code {Foo()} will run the function Foo and get its output. You can also put variables here, such as having var myfoo = Foo(); on the line above, then {myfoo} will resolve to the same output.

Comments

0

You can create a element using the DOM api and set its .innerText property to the value returned by the Foo() function:

function Bar() {
  const boldElem = document.createElement("B");
  return boldElem.innerText = Foo()
}

Comments

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.