Home > Back-end >  How to submit empty value and make it send None as a string react
How to submit empty value and make it send None as a string react

Time:10-08

I am trying to send an empty value and when I submit it, it send "None" to the data.

Here is my code base:

  const [addLoanClause, setAddLoanClause] = useState("");

  const handleSubmit = () => {

if (id) {
  var loanProposalDetailsUpdateObject = {
    ...
    addLoanClause: addLoanClause,
  };
  if (dataChanged(loanProposalDetailsUpdateObject)) {
    updateUserLoanProposalRequest({
      loanProposalDetails: loanProposalDetailsUpdateObject,
    }).then(() => {
      routeToNextPage(isUserLoanRequestInitiator, router, id);
    });
  }
  else {
    routeToNextPage(isUserLoanRequestInitiator, router, id);
  }
} else {
  props
    .createUserLoanRequest({
      requestType: transactionType,
      status: "draft",
      loanProposalDetails: {
        ...
        addLoanClause: addLoanClause,
      },
    })
    .then(data => {
      routeToNextPage(isUserLoanRequestInitiator, router, data.id);
    });
   }
 };

<DynamicWidthInput
  id="addLoanClause"
  value={addLoanClause}
  type="textarea"
  placeholder="1000 characters"
  error={showErrors && getError("addLoanClause")}
  onChange={e =>
    handleDynamicStateChange(e, setAddLoanClause)
  }
  size={"xxl"}
  rows="5"
/>

How can I achieve it if I want to send empty value in textarea and it will automatically know to send string "None" to the data?

CodePudding user response:

You could define your value to be sent using a ternary referencing the length of the input:

const output = addLoanClause.length > 0 ? addLoanClause : 'None'

So if the length of the input is greater than zero, use the input. Else, use 'None'

  • Related