Home > front end >  How to call or use a .js file using onclick Reactjs functional component
How to call or use a .js file using onclick Reactjs functional component

Time:01-18

I have .js file called Edit which is used for edit purpose. I have another .js file called Add. I have a dialog box in edit.js which will open when I click a button. But I don't want to use a button instead I want to get that a dialog box when I click anywhere on box. I tried using onclick in div tag but I didn't get any response. this is the output so if you observe we got a edit button there if click it I am getting form/dialog box for editing the content. but I want that form or dialog box to open when I click anywhere on the yellow box.

<div id='color' className='div2' key={item.id} style={{width: 340,
# border: '5px solid white',textIndent:-30,paddingRight:32,paddingLeft:40,whiteSpace:'pre',paddingTop:15, backgroundColor:item.currentColor}} onClick={()=>{editpage(item.id)}} >

this is what I used for calling function for getting form in another .js file. It is part of a mapping function.` there is a onclick event I used that whenever I click on the box or content which is all under div tag I need to go to that function and there go to edit and then form but it didn't work

the function to which it goes is this:

const editpage=(id)=>{ <Edit id={id}></Edit> }

I want to send a id as a parameter which is passed to Edit.js. I used <Edit/> because it is a another js file which I am importing in Add.js file.

I am not able to get the output can you please me with this. how I can use this when I click on color box should open a form which is indeed in another file.

CodePudding user response:

you can use onClick with <div> it is possible. maybe you are using it the wrong way

<div onClick={() => {
// your code here
}
}>... </div>

CodePudding user response:

This is successfully calling a function on the click event:

onClick={()=>{editpage(item.id)}}

But what does that function do? Absolutely nothing:

const editpage=(id)=>{ <Edit id={id}></Edit> }

The function doesn't return anything (not that returning from a click handler does anything either of course), doesn't perform any operations, nothing. It's just a single JSX expression.

Take a different approach entirely. Add your component to the markup, but wrap it in a condition based on state. For example, consider this state value:

const [openEditPage, setOpenEditPage] = useState();

The purpose of this state value is to keep track of which item.id should currently have its "edit" component visible. (Default to undefined so none are visible by default.)

Use this value when rendering the components:

<div id='color' className='div2' key={item.id} style={/*...*/} onClick={()=>{editpage(item.id)}}>
  {openEditPage === item.id ? <Edit id={item.id}></Edit> : null}
</div>

So in this case the <Edit> component will only be rendered if openEditPage === item.id. Which is what the editpage() function can do:

const editpage = id => setOpenEditPage(id);

Clicking on the <div> would invoke the editpage function, which updates the state value, which triggers a re-render, and in the new render the <Edit> component is shown because the condition would be true.

  • Related