【问题标题】:How to submit data in todo using react hooks如何使用 React hooks 在 todo 中提交数据
【发布时间】:2021-02-02 07:17:07
【问题描述】:

我想使用 React 钩子重构我的 todo 应用程序, 一旦用户输入标题和消息,它们就会保存在一个数组中。

const Messages = () => {
 
   const [messages, saveMessages] = useState([
       {
           title: 'Dan',
           message: 'Hola !'
       }
   ]);

 const [post, setPost] = useState('')
 const [title, setTitle] = useState('')
 
 
const handleSubmit = () => { 
    saveMessages([...messages, post, title])
}

        return (
            <Col lg={9} md={9} sm={12} style={{border: '1px solid grey', paddingTop: '20px'}}>
            <div style={{display: 'flex', justifyContent: 'start', alignItems: 'center'}}>
                <input value={title} onChange={ (e) => setTitle(e.target.value)} className='mr-3 textField' placeholder='your title ...' style={{padding: '5px 5px',  borderRadius: '50px', width: '60%', boxShadow: '0px 4px 16px rgba(17,17,26,0.1), 0px 8px 24px rgba(17,17,26,0.1), 0px 16px 56px rgba(17,17,26,0.1)'}}/>
                <textarea value={post} onChange={ (e) => setPost(e.target.value)} className='mr-3 textField' placeholder='your message ...' style={{padding: '5px 20px',  borderRadius: '50px', width: '60%', boxShadow: '0px 4px 16px rgba(17,17,26,0.1), 0px 8px 24px rgba(17,17,26,0.1), 0px 16px 56px rgba(17,17,26,0.1)'}}/>
                <Button variant="contained" color="primary" onClick={handleSubmit}>
                    Add post
                </Button>
             
            </div>
            <div>
                {/* <Post message={this.state.message} /> */}
                <Post title='Ksena' image='https://i.pinimg.com/originals/cb/7c/6a/cb7c6afd13741ce6a58c1584d8b59097.jpg' message='Hello there !'/>
                <Post title='Josh' image='https://www.pauldavidsmith.co.uk/wp-content/uploads/2019/08/corporate-portraits-peterborough-48-705x529.jpg' message='Welcome !'/>
                {messages.map(i => {
                    return (
                        <Post title={i.title} message={i.message}/>
                    )
                   
                })}
            </div>
            </Col>
        )
  
}

但它只在提交后渲染图像(点击“添加帖子”按钮) 我想知道我是否应该创建 2 个挂钩/数组来保存消息和标题?

【问题讨论】:

  • 更新你的代码到saveMessages([...messages, { message: post, title }])里面handleSubmit
  • 为了正确更新您的状态值,您必须使用创建它们时声明的正确方法,const [post, setPost] = useState(''); 所以为了成功更新说post,您必须使用setPost('...') 和以此类推

标签: reactjs react-hooks jsx


【解决方案1】:
const handleSubmit = useCallback(() => { 
  saveMessages([
    ...messages,
    {
      message: post,
      title: title,
    },
  ]);
}, [messages, post, title]);

更新

更正确地将更新函数传递给您的saveMessages 方法:

const handleSubmit = useCallback(() => { 
  saveMessages((prevMessages) => [
    ...prevMessages,
    {
      message: post,
      title: title,
    },
  ]);
}, [post, title]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2023-02-18
    • 2023-02-24
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多