0

I am new to React.js. I am trying to disable button using props value. This is how I am trying to do but its not working throwing an error Parsing error: Unexpected token, expected "...". Is it the correct way to do this? Please help me. Thanks

import React from "react";

function Note(props) {

  function handleClickUpdateA() {
    props.onUpdateA(props.id);
  }

  var dis;

  if(props.des === '1')
  {
    dis = 'disabled';
  } else
  {
    dis = ' ';
  }

  return (

    <div>
      <p>{props.content}</p>
      <button {dis} onClick={handleClickUpdateA}>Accept</button>
    </div>
  );
}

export default Note;

3 Answers 3

1

button element gets a disabled prop. You can set it true for disabling it. And false to able it. So you just need to change your jsx like below.

JSX Part:

<button disabled={dis === 'disabled'} onClick={handleClickUpdateA}>Accept</button>

But I recommed to use a isDisabled variable and set that to true or false instead of dis with string value. It is a best practice I think.

So your code would become like this:

JS Part:

var isDisabled = props.des === '1' ? true : false;

JSX Part:

<button disabled={isDisabled} onClick={handleClickUpdateA}>Accept</button>

It is more readable now.

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

Comments

0

you can use this to check whether the button is active or not.

 import React from "react";

    function Note(props) {

      function handleClickUpdateA() {
        props.onUpdateA(props.id);
      }

      var dis;

      if(props.des === '1')
      {
        dis = 'disabled';
      } else
      {
        dis = ' ';
      }

      return (

        <div>
          <p>{props.content}</p>
          <button 
          disabled = { props.des === 1 ? true : false  //Your props coming here. and you can return true or false based on the value that comes with props. in this case you will not need to install the if else structure above}
         />
        </div>
      );
    }

    export default Note;

Comments

0
import React from "react";
function Note(props) {
  function handleClickUpdateA() {
    props.onUpdateA(props.id);
  }
  var dis;

  if(props.des === '1')
  {
    dis = true';
  } else
  {
    dis = false;
  }

  return (

    <div>
      <p>{props.content}</p>
      <button disabled={dis} onClick={handleClickUpdateA}>Accept</button>
    </div>
  );
}

export default Note;

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.