[英]How to call a function in a function based ReactJS component?
In ReactJS would like to have a component from a function, and send a call to a function when a form is posted.在 ReactJS 中,希望从函数中获得一个组件,并在发布表单时发送对函数的调用。 This example doesn't work.
这个例子不起作用。 How can be fixed?
如何修复?
import React from 'react';
import Link from "react-router-dom/Link";
function AddPhoto(props) {
return (<div>
<h1>Pickture</h1>
<Link className='cancelIcon' to="/"> </Link>
<div>
<form className="form" onSubmit={e => this.handleSubmit(e)}>
<input className='.form input' type='text' placeholder='Link' name='link'/>
<input className='.form input' type='text' placeholder='Description' name='description'/>
<button> post</button>
</form>
</div>
</div>)
}
export function handleSubmit(event) {
console.log("Form has been submitted");
console.log(event.target.element.link.value);
}
I think there are several issues with your code.我认为您的代码有几个问题。
AddPhoto
component instead of your handleSubmit
function.AddPhoto
组件而不是您的handleSubmit
函数。 Assuming that your file is only exporting your AddPhoto
component, you don't have to declare a constant and you can straightaway export default your AddPhoto
function.AddPhoto
组件,您不必声明一个常量,您可以直接导出默认的AddPhoto
函数。handleSubmit
function.handleSubmit
函数的原因。 So I will include that function inside your AddPhoto
component.AddPhoto
组件中包含该函数。-
in your this.handleSubmit
function, also functional component don't have to use this
, furthermore your handleSubmit
function is not within your functional component's scope.this.handleSubmit
函数中有一个-
,功能组件也不必使用this
,而且您的handleSubmit
函数不在您的功能组件范围内。 So it won't work..
.
in your input className
props as well.className
道具中也是如此。 Here are the codes for you to refer.以下是代码供您参考。
const AddPhoto = (props) => {
const handleSubmit = (event) => {
// Add this if you want to prevent the page from reloading and updating your url
event.preventDefault();
console.log("Form has been submitted");
// Assuming you want to get the `link` field's value, you can get it by using this
console.log(event.target.link.value);
}
return (
<div>
<h1>Pickture</h1>
<Link className="cancelIcon" to="/">
{" "}
</Link>
<div>
<form className="form" onSubmit={handleSubmit}>
<input
className="form input"
type="text"
placeholder="Link"
name="link"
/>
<input
className="form input"
type="text"
placeholder="Description"
name="description"
/>
<button type="submit">post</button>
</form>
</div>
</div>
);
}
export default AddPhoto;
this
from your handleSubmit since it's declared outside.this
,因为它是在外面声明的。 Remove export
if you're declaring the function in the same file.如果您在同一文件中声明函数,请删除
export
。 Otherwise, you would have to define your function in a separate file and then import from there.否则,您必须在单独的文件中定义您的函数,然后从那里导入。
function AddPhoto(props) { return (<div> <h1>Pickture</h1> <Link className='cancelIcon' to="/"> </Link> <div> {/* Removed this.*/} <form className="form" onSubmit={e => handleSubmit(e)}> <input className='.form input' type='text' placeholder='Link' name='link'/> <input className='.form input' type='text' placeholder='Description' name='description'/> <button> post</button> </form> </div> </div>) } // Removed export function handleSubmit(event) { console.log("Form has been submitted"); console.log(event.target.element.link.value); }
There are quite some issue with your code:您的代码有很多问题:
this
because first of all this
keyword is only used in classes to bind functions to the class.this
因为首先this
关键字仅在类中用于将函数绑定到类。 So because AddPhoto is an functional component, you don't have to bind the function to the class.event.preventDefault();
event.preventDefault();
, because otherwise the page will reload (to submit the form) and you will lose your state of your application. import React from 'react';
import Link from 'react-router-dom/Link';
export default function AddPhoto() {
const handleSubmit = event => {
event.preventDefault();
const link = event.target.link;
const description = event.target.description;
console.log('Form has been submitted');
};
return (
<div>
<h1>Picture</h1>
<Link className="cancelIcon" to="/">
cancel
</Link>
<div>
<form className="form" onSubmit={handleSubmit}>
<input
className="form-input"
type="text"
placeholder="Link"
name="link"
ref={linkInput}
/>
<input
className="form-input"
type="text"
placeholder="Description"
name="description"
ref={descriptionInput}
/>
<button type="submit">post</button>
</form>
</div>
</div>
);
}
Try this:尝试这个:
import React from 'react';
import Link from "react-router-dom/Link";
export function AddPhoto(props) {
return (<div>
<h1>Pickture</h1>
<Link className='cancelIcon' to="/"> </Link>
<div>
<form className="form" onSubmit={handleSubmit}>
<input className='.form input' type='text' placeholder='Link' name='link'/>
<input className='.form input' type='text' placeholder='Description' name='description'/>
<button> post</button>
</form>
</div>
</div>)
}
function handleSubmit(event) {
console.log("Form has been submitted");
console.log(event.target.element.link.value);
}
I think you should be needing to write something like ...我认为你应该需要写一些像......
import React from 'react';
import Link from "react-router-dom/Link";
export const AddPhoto = props => {
const handleSubmit = e =>{
return(
console.log("Form has been submitted");
console.log(e.target.element.link.value);
)
}
return (<div>
<h1>Pickture</h1>
<Link className='cancelIcon' to="/"> </Link>
<div>
<form className="form" onSubmit={e => handleSubmit(e)}>
<input className='.form input' type='text' placeholder='Link' name='link'/>
<input className='.form input' type='text' placeholder='Description' name='description'/>
<button> post</button>
</form>
</div>
</div>)
}
AddPhoto.defaultProps = {
onSubmit: ()=>{}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.