1

I am creating a react app and getting all data from API calls. I have set data into an array and display them. And there is data coming with an input tag and there is a function inside the tag. How can I call that functions in react js?

My data set is like below.

qtemp3: [
  {
    idappcontent: "Sign_01",
    content:
      '<img id="vClub_logo" src="#FOLDERPATH#v-club.png" style="width:600px;height: 150px;">',
  },
  {
    idappcontent: "Sign_02",
    content:
      "<div style=\"color: #0D0D0D;\"> When you give us your name,<br /> e-mail address and the dates of your birthday and anniversary, we'll add you to our exclusive email database. You'll then be entitled to receive valuable offers each month and on those special occasions.</div>",
  },
  {
    idappcontent: "Sign_03",
    content:
      ' <div style="text-align:center"> <label for="firstName">First Name</label></div><div style="margin-top: 3%;"><input id="fname" name="firstName" onkeyup="FirstnameValidation()" style="font-family: trajanPro;width:100%;text-transform: capitalize;border-color: rgb(224, 94, 7);" /> </div>',
  },
  {
    idappcontent: "Sign_04",
    content:
      '<div style="text-align:center"> <label for="lname">Last Name</label> </div><div style="margin-top: 3%;"><input id="lname" name="lname" style="font-family: trajanPro;width:100%;text-transform: capitalize;" /></div>',
  },
  {
    idappcontent: "Sign_05",
    content:
      ' <div style="text-align:center"><label for="email">Email Address</label></div><div style="margin-top: 1%;"><input id="email" type="email" name="email" onkeyup="emailvalidation()"  style="width:100%;border-color: rgb(224, 94, 7);" /></div>',
  },
  {
    idappcontent: "Sign_06",
    content:
      '<div id="birthdayHeader"><label>Birthday</label></div><div style="display: inline-table; margin-top: 8px; width: 100%;"><div style="display: inline-block; width: 50%; text-align: right"><label for="birthdayMonth">(Month)</label><input id="birthdayMonth" type="number" min="0" max="12" onkeyup = "birthdayMonthValidate()"  name="birthdayMonth" style="width:45px"></div><div style="display:inline-block;width:50%;text-align:left"><input id="birthdayDay" type="number" min="0" max="31" name="birthdayDay" onkeyup = "birthdayValidate()" style="width:45px"><label for="birthdayDay">(Day)</label></div></div></div>',
  },
],

This is what I tried;

import React, { Component } from "react";

export default class testPage3 extends Component {
  state = {
    surveyContent: [],
    qtemp3: [
      {
        idappcontent: "Sign_01",
        content:
          '<img id="vClub_logo" src="#FOLDERPATH#v-club.png" style="width:600px;height: 150px;">',
      },
      {
        idappcontent: "Sign_02",
        content:
          "<div style=\"color: #0D0D0D;\"> When you give us your name,<br /> e-mail address and the dates of your birthday and anniversary, we'll add you to our exclusive email database. You'll then be entitled to receive valuable offers each month and on those special occasions.</div>",
      },
      {
        idappcontent: "Sign_03",
        content:
          ' <div style="text-align:center"> <label for="firstName">First Name</label></div><div style="margin-top: 3%;"><input id="fname" name="firstName" onkeyup="FirstnameValidation()" style="font-family: trajanPro;width:100%;text-transform: capitalize;border-color: rgb(224, 94, 7);" /> </div>',
      },
      {
        idappcontent: "Sign_04",
        content:
          '<div style="text-align:center"> <label for="lname">Last Name</label> </div><div style="margin-top: 3%;"><input id="lname" name="lname" style="font-family: trajanPro;width:100%;text-transform: capitalize;" /></div>',
      },
      {
        idappcontent: "Sign_05",
        content:
          ' <div style="text-align:center"><label for="email">Email Address</label></div><div style="margin-top: 1%;"><input id="email" type="email" name="email" onkeyup="emailvalidation()"  style="width:100%;border-color: rgb(224, 94, 7);" /></div>',
      },
      {
        idappcontent: "Sign_06",
        content:
          '<div id="birthdayHeader"><label>Birthday</label></div><div style="display: inline-table; margin-top: 8px; width: 100%;"><div style="display: inline-block; width: 50%; text-align: right"><label for="birthdayMonth">(Month)</label><input id="birthdayMonth" type="number" min="0" max="12" onkeyup = "birthdayMonthValidate()"  name="birthdayMonth" style="width:45px"></div><div style="display:inline-block;width:50%;text-align:left"><input id="birthdayDay" type="number" min="0" max="31" name="birthdayDay" onkeyup = "birthdayValidate()" style="width:45px"><label for="birthdayDay">(Day)</label></div></div></div>',
      },
    ],
  };

  

  render() {
    const { qtemp3} = this.state;
    return qtemp3.map((item, index) => (
     <div key={item.idappcontent}>
       <div dangerouslySetInnerHTML={{ __html: item.content }} />
     </div>
   ));
  }
}

How to call these function in react js? onkeyup="FirstnameValidation()" , onkeyup="emailvalidation()" . Cant change the data set coming from API. I also post a question related to this -> Check this.

Sidath
  • 379
  • 1
  • 9
  • 25
  • `const { qtemp3} = this.state;` this should be outside the render loop. apart from that, what is the issue? – boxdox Aug 11 '21 at 05:40
  • I need to call the function inside the input tag. the data display correctly. now I need to access the functions inside the input tags. there are onkeyup function "FirstnameValidation()" , "emailvalidation()". need to call these functions. – Sidath Aug 11 '21 at 05:44
  • 1
    I don't think React will handle setting up listeners for you in that case. You need to manually extract those functions and re-attach them to the element. I might be wrong – boxdox Aug 11 '21 at 05:47
  • How can I do that? – Sidath Aug 11 '21 at 05:50
  • https://stackoverflow.com/questions/30523800/call-react-component-function-from-onclick-in-dangerouslysetinnerhtml try this maybe – boxdox Aug 11 '21 at 05:51
  • There is not a good answer for me. If there is a way to fix this please help me. I am new to react js. – Sidath Aug 11 '21 at 06:06

0 Answers0