0
const templateTwo = (
    <div>
    <h1>Count :{count}</h1>
    <button  className="button" onClick={minusOne}>-1</button>
    <button onClick={function name() {
      console.log("nnn")
    }}>Reset</button>
    </div>
)

1) According to the JSX

You can put any valid JavaScript expression inside the curly braces in JSX.

2) but while reading docs for eventHandler in React i came across the following

 JSX you pass a function as the event handler, rather than a string.

3) Until here everyThing looks great but now in my above code i had used functional statement as EventHandler but it worked i am confused as per docs we have to use functional expressions inside {}

4) How it worked can we use functional statments inside {} (in jsx )

0

2 Answers 2

1

Depending on the context, function name() { } could be either a function declaration or a function expression.

var a = function name() { };  // <-- function expression

function name() { }           // <-- function declaration

JSX expects an expression within the {} here, so it is interpreted as a function expression.

You could also do this, with an arrow function (which is surely more common):

<button onClick={() => {
  console.log("nnn")
}}>Reset</button>

For all intents and purposes this is essentially the same as your version with the function keyword.

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

8 Comments

So you mean if i pass a functional statment also it evaluates to functional expression correct me if i am wrong
@m.dvenkatesh I'm not really sure what you mean by "functional statement". Contexts that expect an expression will treat function name() { } as an expression because that is valid syntax for a function expression.
correct me if i am wrong i am referring to functional Declartion as functional statement
@m.dvenkatesh Ok. As I said above, that syntax could be a declaration or an expression depending on the situation. In this situation, it's an expression. This is not something that is specific to React.
@m.dvenkatesh MDN documentation: here, JavaScript.info: here
|
1

The code you use is an expression.

// Function declaration:
function doStuff() {};

// Function expression:
const doStuff = function() {}

So, you're using a function expression and works just fine as others:

onClick={function name() {console.log("nnn")}}

Read more about function expression.


Further more,

This statement is invalid:

<a onClick={const doStuff = function doStuff(){console.log('nnn')}}>test</a>

This expression is valid:

<a onClick={doStuff = function doStuff(){console.log('nnn')}}>test</a>

The function statement is related to variable hoisting while function expression is related to resolving value. So, you can use anything in jsx that resolves the value.

1 Comment

Thanks it means anyThing in {} in jsx will resove to value so we can use anything (functional Statment or functional Ecpression)if we don't resove to value then it is a problem

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.