【发布时间】:2021-07-11 03:45:58
【问题描述】:
我正在构建一个博客应用程序。这个应用程序的前端是用 ReactJS 构建的,后端是用 Python(Django 框架)构建的。前端和后端通过 Django REST API 框架连接。
这篇文章在 ReactJS 前端。
我正在使用功能组件。有一个“BlogList”组件,其中将显示从 API 端点获取的所有博客。我创建了一个“BlogForm”组件,我必须在其中输入 2 个字段:“标题”和“内容”来创建博客文章。我在“BlogList”组件中导入了“BlogForm”组件以在同一页面上显示它。 Please see this image to get a good understanding
当我在“BlogForm”组件中输入“标题”和“内容”并单击“提交”按钮时,数据使用 API 调用保存在数据库中。但当时添加的数据并未显示在“BlogList”组件中。我必须刷新页面才能在博客列表中看到新的博文。
社区中的任何人都可以帮助我解决单击“提交”按钮时如何立即查看添加的博客文章的问题吗?
代码如下。
BlogList.js
import BlogForm from './BlogForm'
const BlogList = (url) => {
const [blogs, setBlogs] = useState([])
useEffect(() => {
async function fetchBlogData() {
const url = requests.blogList
const request = await axios.get(url)
setBlogs(request.data)
return request
}
fetchBlogData()
}, [url])
return (
<Wrapper>
<BlogWrapper className="blog">
// Blog Form Component Added
<BlogForm />
<div className="blog__header">
<h1 className="blog__header--title">
Information
</h1>
</div>
<hr className="blog__divider"/>
<div className="blog__list">
<ul>
<li className="blog__item">
{ blogs.map( (blog) => (
<div className="blog__item--container" key={ blog.id }>
<h1 className="blog__item--title">
<a className="blog__item--title blog__item--title--link" href={`/blog/${ blog.id }`}>
{ blog.title }
</a>
</h1>
<small className="blog__item--date">{ blog.date_published }</small>
<p className="blog__item--content">{ blog.content }</p>
</div>
) ) }
</li>
</ul>
</div>
</BlogWrapper>
</Wrapper>
)
}
export default BlogList
BlogForm.js
const BlogForm = () => {
const [inputValues, setInputValues] = useState({
title : '',
content : ''
})
const handleSubmit = async (event) => {
event.preventDefault()
setInputValues({ ...inputValues })
const { title, content } = inputValues
const blogPost = { title, content }
const url = requests.blogCreate
const response = await axios.post(url, blogPost)
return response
}
const handleChange = (name) => (event) => {
setInputValues({ ...inputValues, [name] : event.target.value })
}
return (
<div>
<form onSubmit={ handleSubmit }>
<input type="text" name="title" placeholder="Title" required value={ inputValues.title } onChange={ handleChange('title') }/>
<input type="text" name="content" placeholder="Content" required value={ inputValues.content } onChange={ handleChange('content') }/>
<button type="submit">Submit</button>
</form>
</div>
)
}
export default BlogForm
更新
【问题讨论】:
-
你能演示一下这两个组件是如何相互关联的,它们是如何呈现的吗?据我所知,您只需更新
BlogList中的props对象(您将其命名为url),它应该会重新渲染并触发效果回调。 -
@DrewReese:我只是在
BlogList组件中导入BlogForm组件。 -
您需要 BlogList 中的某种回调函数,将其作为道具传递给 BlogForm。
-
对不起,如果不清楚,我指的是响应正文,而不是请求状态,换句话说,你在代码中得到了什么。 GET 请求中的
console.log(request.data)和 POST 请求中的console.log(response)之类的东西。 -
我得到了解决方案。它应该是 POST 请求中的 response.data。非常感谢@DrewReese
标签: reactjs use-effect use-state react-functional-component