简体   繁体   English

onClick:从 componentDidMount 调用 function

[英]onClick :call a function from componentDidMount

I have a form like this:我有这样的表格:

<form id="sendSms">
            <h1  className="distance">signup</h1>
            <input className="text-right" onClick={ostype} id="input1" name="mobileNumber" pattern="[0-9]+" type="text" placeholder="mobile" required />
            <input name="osVersion" id="input2" type="hidden" value="" />
            <input name="osType" id="input3" type="hidden" value="" />
            <input name="deviceID" id="input4" type="hidden" value="Come on" />

            <button className="distance" type="submit" onClick={sendData}>submit</button>
        </form>

I want to sendData function POST the inputs to api我想发送sendData function 将输入 POST 到 api

the function: function:

  function sendData() {
    axios({
        method: 'post',
        url: 'some api i have',
        data: sendSms,
    })

    .then(res => this.setState({ recipes: res.data }));

}

var sendSms = 
{
    'mobileNumber' : document.getElementById("input1"),
    'osVersion' : document.getElementById("input2"),
    'osType' : document.getElementById("input3"),
    'deviceID' : document.getElementById("input4"),
}

I tried to put axios in componentDidMount() but that way i cant call the sendData functuion with onClick. and when i put the function in my code, i get NS_BINDING_ABORTED我试图将 axios 放入componentDidMount() ,但那样我就无法使用 onClick 调用sendData函数。当我将 function 放入我的代码时,我得到NS_BINDING_ABORTED

Try something like this:尝试这样的事情:

// Using Hooks
export default function SMSView() {
    const [state, setState] = useState({
        mobileNumber: '',
        osVersion: '',
        osType: '',
        deviceID: ''
    })

    const handleChange = ({target: {value, name}}) => {
        setState({...state, [name]: value})
    }

    const handleSubmit = e => {
        e.preventDefault()

        // axios here
    }

    return <form onSubmit={handleSubmit}>
        <input name="mobileNumber" value={state.mobileNumber} onChange={handleChange}/>
        <input name="osVersion" value={state.osVersion} onChange={handleChange}/>
        <input name="osType" value={state.osType} onChange={handleChange}/>
        <input name="deviceID" value={state.deviceID} onChange={handleChange}/>
        <button type="submit">Submit</button>
    </form>
}

// Using Class Component
export default class SMSView extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            mobileNumber: '',
            osVersion: '',
            osType: '',
            deviceID: ''
        }
    }

    handleChange = ({target: {value, name}}) => {
        this.setState({...this.state, [name]: value})
    }

    handleSubmit = e => {
        e.preventDefault()

        // axios here
    }

    render() {
        const {mobileNumber, osVersion, osType, deviceID} = this.state
        return <form onSubmit={this.handleSubmit}>
            <input name="mobileNumber" value={mobileNumber} onChange={this.handleChange}/>
            <input name="osVersion" value={osVersion} onChange={this.handleChange}/>
            <input name="osType" value={osType} onChange={this.handleChange}/>
            <input name="deviceID" value={deviceID} onChange={this.handleChange}/>
            <button type="submit">Submit</button>
        </form>
    }
}

or use libraries like [React Hook Form][1]或使用像 [React Hook Form][1] 这样的库

[1]: https://react-hook-form.com/

You have mixed two worlds in your code: In the below code(that you have written) to get the Values from the inputs, The approach is Imperative but still it is wrong, You are not assigning any value but directly assigning DOM elements to your sendSms object.您在代码中混合了两个世界:在下面的代码(您编写的)中从输入中获取值,该方法是Imperative的,但仍然是错误的,您没有分配任何值,而是直接将 DOM 元素分配给您的sendSms

var sendSms = 
{
    'mobileNumber' : document.getElementById("input1"),
    'osVersion' : document.getElementById("input2"),
    'osType' : document.getElementById("input3"),
    'deviceID' : document.getElementById("input4"),
}

You should use a Controlled Component Approach instead to get values of your inputs.您应该使用受控组件方法来获取输入值。

Secondly, There should not be any error at all if you use onClick the way you have, You do not need to use ComponentDidMount at all.其次,如果你按照你的方式使用 onClick 应该不会有任何错误,你根本不需要使用ComponentDidMount

You simply should check two things,before sending your data via Post Axios request.在通过 Post Axios 请求发送数据之前,您只需检查两件事。

  1. Whether I have the available data from inputs.我是否有来自输入的可用数据。
  2. Whether server is allowing for Post call.服务器是否允许 Post 调用。

So, you need to do some homework dear Friend, Let me share some resources.所以,亲爱的朋友,你需要做一些功课,让我分享一些资源。

From Official React Docs Controlled Forms来自 React 官方文档控制 Forms

Making post call (on Form submit exactly what you require) https://reactgo.com/react-post-request-axios/拨打电话(在表格上准确提交您需要的内容) https://reactgo.com/react-post-request-axios/

Please Note: Though Class based approach will work but from React 16.8, React is officially using React Hooks, do have a look into that as well https://reactjs.org/docs/hooks-intro.html请注意:虽然基于 Class 的方法可行,但从 React 16.8 开始,React 正式使用 React Hooks,也请查看https://reactjs.org/docs/hooks-intro.html

For onClick event you must to do like this:对于 onClick 事件,您必须这样做:

onClick={() => sendData()}

After I advise you to use Function components instead of Class components and use Hook instead directly use state and lifecycle fonction.在我建议你使用 Function 组件而不是 Class 组件并使用 Hook 而不是直接使用 state 和生命周期功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM