在React

时间:2016-10-17 22:58:38

标签: javascript forms reactjs

我目前正在构建一个简单的React应用程序。我从laravel应用程序传递了一些数据传递给window.example对象。然后我想访问数据,我发现我最初可以使用props来完成。但问题是,当我提交表单时,在AssignmentForm组件中,我想更新AssignmentBox中的数据,它显示数据,并添加一行数据。我该怎么做?

所以我的结构看起来像这样:

  • DashboardApp
    • AssignmentBox
    • AssignmentFormNew

这是我的代码:

main.js:

import swal from 'sweetalert';
import $ from 'jquery';
import React from 'react';
import { render } from 'react-dom';
import DashboardApp from './components/Dashboard';

render(
    <DashboardApp tracks={window.tracks} assignments={window.assignments} />,
    document.getElementById('content')
);

Dashboard.js:

import React from 'react';
import {Grid, Row, Col} from 'react-bootstrap';
import TrackBox from './TrackBox';
import TrackFormStop from './TrackFormStop';
import TrackFormNew from './TrackFormNew';
import AssignmentBox from './AssignmentBox';
import AssignmentFormNew from './AssignmentFormNew';

class DashboardApp extends React.Component {
    render () {
        return (
            <Grid>
                <Row>
                    <Col md={12}>
                        <AssignmentBox assignments={this.props.assignments} />
                        <AssignmentFormNew />
                    </Col>
                </Row>
            </Grid>
        )
    }
}

export default DashboardApp;

AssignmentBox.js

import React from 'react';
import {Panel} from 'react-bootstrap';

const title = (
  <h2>Current Assignments</h2>
);

class AssignmentBox extends React.Component {
    render () {
        return (
            <Panel header={title}>
                <ul>
                    {this.props.assignments.map(item => (
                        <li key={item.id}><a href="assignment/{item.slug}">{item.title}</a></li>
                    ))}
                </ul>
            </Panel>
        )
    }
}

export default AssignmentBox;

AssignmentFormNew.js

import React from 'react';
import {Panel, Button, FormGroup, ControlLabel} from 'react-bootstrap';
import $ from 'jquery';

const title = (
    <h2>New Assignment</h2>
);

const footer = (
    <Button bsStyle="primary" type="submit" block>New Assignment</Button>
);

class AssignmentFormNew extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            title: '',
            description: '',
            customer: '',
            date: ''
        };

        this.handleSubmit = this.handleSubmit.bind(this);

        this.handleTitleChange = this.handleTitleChange.bind(this);
        this.handleDescriptionChange = this.handleDescriptionChange.bind(this);
        this.handleCustomerChange = this.handleCustomerChange.bind(this);
        this.handleDeadlineChange = this.handleDeadlineChange.bind(this);
    }

    handleSubmit (e) {
        e.preventDefault();

        console.log(window.Laravel.csrfToken);

        $.ajax({
            url: '/assignment',
            type: 'POST',
            data: {
                _token: window.Laravel.csrfToken,
                title: this.refs.title.value,
                description: this.refs.description.value,
                customer: this.refs.customer.value,
                deadline: this.refs.deadline.value
            },
            success: res => {
                this.setState({
                    title: '',
                    description: '',
                    customer: '',
                    deadline: ''
                });

                console.log(res);
            },
            error: (xhr, status, err) => {
                console.error(status, err.toString());
            }
        });
    }

    handleTitleChange (event) {
        this.setState({title: event.target.value});
    }

    handleDescriptionChange (event) {
        this.setState({description: event.target.value});
    }

    handleCustomerChange (event) {
        this.setState({customer: event.target.value});
    }

    handleDeadlineChange (event) {
        this.setState({deadline: event.target.value});
    }

    render () {
        return (
            <form onSubmit={this.handleSubmit}>
                <Panel header={title} footer={footer}>
                    <FormGroup controlId="assignmentTitle">
                        <ControlLabel>Title</ControlLabel>
                        <input type="text" ref="title" placeholder="e.g. Crowdfunding module for prestashop" className="form-control" value={this.state.title} onChange={this.handleTitleChange} />
                    </FormGroup>
                    <FormGroup controlId="assignmentDescription">
                        <ControlLabel>Description</ControlLabel>
                        <textarea className="form-control" ref="description" rows="10" value={this.state.description} onChange={this.handleDescriptionChange} />
                    </FormGroup>
                    <FormGroup controlId="assignmentCustomer">
                        <ControlLabel>Customer</ControlLabel>
                        <input type="text" placeholder="e.g. John Doe" ref="customer" className="form-control" value={this.state.customer} onChange={this.handleCustomerChange} />
                    </FormGroup>
                    <FormGroup controlId="assignmentDeadline">
                        <ControlLabel>Deadline</ControlLabel>
                        <input type="date" className="form-control" ref="deadline" value={this.state.deadline} onChange={this.handleDeadlineChange} />
                    </FormGroup>
                </Panel>
            </form>
        )
    }
}

export default AssignmentFormNew;

提前谢谢。

3 个答案:

答案 0 :(得分:1)

在Dashboard.js中放置handleSubmit()函数,并添加以下代码

.

然后在AssignmentFormNew.js中更改:

 constructor (props) {
        super(props);
        this.state = {
            assignments:this.props.assignments
        };
}

handleSubmit (e) {
  ... your ajax code
  this.setState({assignments:res})
}

<AssignmentBox assignments={this.state.assignments} handleSubmit={this.handleSubmit}/>

基本上当你点击提交时,它会在Dashboard.js中调用父的handleSubmit函数,然后在你的ajax调用之后,更新状态,以便AssignmentBox将使用新数据重新呈现它。

答案 1 :(得分:0)

在赋值框中创建一个方法来更新数据并将该函数作为prop传递给赋值表单。在赋值表单中调用函数并传递数据。

答案 2 :(得分:0)

AssignmentFormNew.js中的

handleSubmit (e) {
    e.preventDefault();
    this.props.childValuesToParent(e);  
  .....
}

现在这个道具在父级内部可用 - Dashboard.js就像这样

<AssignmentFormNew childValuesToParent={this.handleChange} />
有点像回电话。