【问题标题】:Why doesn't the data post为什么数据发不出去
【发布时间】:2023-02-09 07:16:45
【问题描述】:

当我添加 location.assign('AllBlog') 时,它不会发布数据,但如果我删除它,它就会起作用。

import React, { useState } from 'react'
import './PostBlog.css'
import axios from 'axios'

function PostBlog() {


    const [title , setTitle] =useState(null);
    const [body , setBody] =useState(null);
    const [PostDone , setPostDone] =useState(false);

    const handelPost =()=>{
        axios.post('http://127.0.0.1:7000/postBlog',{
            title:title,
            body:body
            })
            setPostDone(()=>true)
    }

    {PostDone ? window.location.assign('/AllBlog'): null}


return (
        <section className='Post-blog'>

            <h1 className='post-header-text'> Post what you Like</h1>

            <div className='form-post'>

            <label>
                <h3>title</h3>
                <input type="text" className='title-from' onChange={(e)=>     {setTitle(e.target.value)}}/>
            </label>
            <label>
                <h3>Pergraph</h3>
                <textarea type="text" className='p-from' rows="6" onChange={(e)=>{setBody(e.target.value)}}></textarea>
            </label>
            {/* <label>
                <h3>Upload Image</h3>
                <input type="file"/>
            </label> */}

            <button className='btn una' onClick={()=>handelPost()}>Post</button>
            </div>

        </section>
    )
}

export default PostBlog

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    问题似乎在于您在成功发布后尝试将用户重定向到“AllBlog”页面的方式。执行此操作的正确方法是使用 react-router-dom 库中的 useHistory 挂钩以编程方式将用户导航到不同的页面。

    这是您的代码的更新版本,其中包含更改:

    import React, { useState } from 'react'
    import './PostBlog.css'
    import axios from 'axios'
    import { useHistory } from 'react-router-dom'
    
    function PostBlog() {
      const history = useHistory()
      const [title, setTitle] = useState(null)
      const [body, setBody] = useState(null)
    
      const handlePost = () => {
        axios.post('http://127.0.0.1:7000/postBlog', {
          title: title,
          body: body
        })
          .then(() => {
            setTitle(null)
            setBody(null)
            history.push('/AllBlog')
          })
      }
    
      return (
        <section>
          <h1 className='post-header-text'>Post what you Like</h1>
          <div className='form-post'>
            <label>
              <h3>title</h3>
              <input type='text' className='title-from' onChange={(e) => { setTitle(e.target.value) }} />
            </label>
            <label>
              <h3>Pergraph</h3>
              <textarea type='text' className='p-from' rows='6' onChange={(e) => { setBody(e.target.value) }}></textarea>
            </label>
            <button className='btn una' onClick={handlePost}>Post</button>
          </div>
        </section>
      )
    }
    
    export default PostBlog
    

    【讨论】:

    【解决方案2】:

    函数 axios.post 是异步的。因此 window.location.assign 可能会在请求完成之前执行。要修复此问题,请使函数 handelPost 异步并将 await 添加到 axios.post。

    const handelPost = async () => {
      await axios.post('http://127.0.0.1:7000/postBlog', {
        title:title,
        body:body
      });
    
      setPostDone(() => true);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 2021-11-15
      • 2017-12-20
      • 1970-01-01
      相关资源
      最近更新 更多