Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs react不是从Submit函数中触发_Reactjs - Fatal编程技术网

Reactjs react不是从Submit函数中触发

Reactjs react不是从Submit函数中触发,reactjs,Reactjs,我试图在submit上提交表单,但它没有触发这个。commentSubmit函数,如果我取出,使用函数它可以工作,但是我需要将表单包装在文本字段周围,以便后端读取请求正文。comment\u正文才能工作 Comment.js import React from "react"; import TextField from '@material-ui/core/TextField'; import Button from '@material-ui/core/Button'; const Com

我试图在submit上提交表单
,但它没有触发
这个。commentSubmit
函数,如果我取出
,使用
函数它可以工作,但是我需要将表单包装在
文本字段
周围,以便后端读取
请求正文。comment\u正文
才能工作

Comment.js

import React  from "react";
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
const Comment = (props) => (
    <form onSubmit={props.onSubmit}>
        <TextField
            type="text"
            id="outlined-multiline-static"
            label="Write A Comment"
            multiline
            name="comment_body"
            value={props.commentBody}
            rows="10"
            fullWidth
            margin="normal"
            variant="outlined"
            onChange={props.commentChange}
        />
        <Button type="submit" variant="outlined" component="span" color="primary">
            Post A Comment
        </Button>
    </form>

)
export default Comment;

问题在于来自物料界面的
不是实际的按钮,而是
的组合。因此,如果您有一个
type=“submit”
表单,则该表单不会执行任何操作

如果将材质ui
更改为本机
,它将按预期工作

下面是一个例子:

import React from "react";
import Button from '@material-ui/core/Button';
import Grid from '@material-ui/core/Grid';
import Typography from '@material-ui/core/Typography';
import Paper from '@material-ui/core/Paper';
import Divider from '@material-ui/core/Divider';
import Image from './Image';
import Axios from '../Axios';
import moment from 'moment';
import Comment from './Comment';

class ImageContainer extends React.Component{
    state = {
      isComment: false,
      comment_body: ""
    }

    handleCommentChange = (e) => {
        this.setState({
           comment_body: e.target.value
        })             
    }

    writeComment = (id)  => {
        this.setState({
            isComment: this.state.isComment ? '' : id // check if you state is filled to toggle on/off comment
        })   
    }

    commentSubmit = (e) => {
        e.preventDefault();
        console.log(this.state.comment_body); // doesn't get console.log
        Axios.post('/images/newComment', this.state.comment_body).then( (response )=> {
            const newComment = { ...response.data};
            console.log(newComment);
            this.setState({
                comment_body: ''
            })
        })
    }

    render(){
       const { img, deleteImg } = this.props
       return(
           <Grid item sm={12} md={12} style={{ margin: '30px 0px'}}>
               <Paper style={{padding:'20px 20px'}}>
         {/* // empty image_title */}
               <Typography style={{ padding: '30px 5px', letterSpacing:'8px', textTransform:'uppercase'}} variant="h4" align="center">{img.image_title}</Typography> 
               <Divider style={{ width: '150px', margin:'10px auto', backgroundColor:'#000000'}} variant="middle" />
               <Image image_url={img.img_url} />   
               <Typography variant="h6" align="center">{img.user.username}</Typography> 
               <Typography variant="h6" align="center">{moment(img.created_at).calendar()}</Typography> 
               <Button onClick ={() => this.writeComment(img.id)} variant="outlined" component="span" color="primary"> 
            {this.state.isComment === img.id ? "Close" : "Write A Comment"}
               </Button>
            {/* here were prevent comments being selected for all items in the array, renders the comment form you clicked on.  */}
            {this.state.isComment === img.id ?
               <Comment onSubmit={this.commentSubmit} 
                            commentBody={this.state.comment_body } 
                            commentChange={this.handleCommentChange}/> 
            : null}
            {/* hide delete button when user enters comment */}
            {!this.state.isComment ? <Button style={{margin: '0px 20px'}} onClick={deleteImg} variant="outlined" component="span" color="primary">
                Delete
            </Button> : null}

            </Paper>                              
        </Grid>        
      )
   }
}

export default ImageContainer
import React  from "react";
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
const Comment = (props) => {
    // <form onSubmit={props.onSubmit}>

    return(
        <div>
        <TextField
            type="text"
            id="outlined-multiline-static"
            label="Write A Comment"
            multiline
            name="comment_body"
            value={props.commentBody}
            rows="10"
            fullWidth
            margin="normal"
            variant="outlined"
            onChange={props.commentChange}
        />
        <Button onClick={props.onSubmit} type="submit" variant="outlined" component="span" color="primary">
            Post A Comment
        </Button>
        </div>
    );
    // </form>

}
export default Comment;
router.post('/newComment', async (req, res) => {
    const comment = new Comment({
        comment_body: req.body.comment_body,
        user_id: req.user.id
    })

    comment.save().then( (comment) => {
        return res.status(200).json(comment);
    })
})