简体   繁体   English

如何在基于函数的 ReactJS 组件中调用函数?

[英]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.我认为您的代码有几个问题。

  1. You probably want to export your 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函数。
  2. I actually don't see the reason to export your handleSubmit function.我实际上没有看到导出handleSubmit函数的原因。 So I will include that function inside your AddPhoto component.因此,我将在您的AddPhoto组件中包含该函数。
  3. There is a - 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.所以它不会工作。
  4. You probably don't need the .您可能不需要. in your input className props as well.在您的输入className道具中也是如此。
  5. I've formatted your code to make it look cleaner :)我已经格式化了您的代码以使其看起来更干净:)

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;
  1. Remove this from your handleSubmit since it's declared outside.从你的 handleSubmit 中删除this ,因为它是在外面声明的。
  2. 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:您的代码有很多问题:

  1. The button doesn't have a type=“submit” on it, so therefor it won't even submit the form when pressed.该按钮上没有 type="submit",因此它在按下时甚至不会提交表单。
  2. HandleSubmit is an function specific to AddPhoto, so it should be inside the AddPhoto component. HandleSubmit 是一个特定于 AddPhoto 的函数,所以它应该在 AddPhoto 组件中。 (I also like to use an arrow function in this case because it should be an anonymous function). (在这种情况下我也喜欢使用箭头函数,因为它应该是一个匿名函数)。
  3. There is no need the use 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.因此,因为 AddPhoto 是一个函数组件,所以您不必将该函数绑定到该类。
  4. You should add 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.

相关问题 如何从reactJs中的另一个组件调用函数 - How to call function from another component in reactJs ReactJS如何从同一文件上的另一个函数调用组件函数? - ReactJS how to call a component function from another function on the same file? 如何在ReactJS中使用参数正确调用父组件中的事件处理函数? - How to correctly call an event handler function in a parent component with a parameter in ReactJS? 我如何在功能组件中调用 function - ReactJs - How do i call function in functional component - ReactJs 如何使用 ReactJS 使用 props 调用另一个组件中的函数? - How to call a function in another component with props using ReactJS? 如何在reactjs中调用无状态功能组件内部的函数 - How can I call a function inside of stateless functional component in reactjs 如何从ReactJS中的onClick函数调用另一个组件 - How to call another component from onClick function in ReactJS 如何在同一组件内的回调中在ReactJS中调用函数? - How to call function in ReactJS in a callback within the same component? 如何从子组件的子组件调用处理程序函数 (ReactJS) - How to call handler function from a child of a child component (ReactJS) Reactjs - 使用功能组件在应用程序启动时调用函数 - Reactjs - Call function on startup of application with a functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM