将值从表单传递到另一个组件

时间:2019-05-10 21:55:56

标签: javascript reactjs web-applications frontend

我有一个带有三个输入字段的Form组件,提交表单时,这些值应传递给Results组件。当我没有提交任何消息时,它们将在控制台上打印,而不在结果组件上打印。

在Results js组件中,我尝试传递道具,但没有任何反应,没有错误消息。有人可以识别我的代码中缺少的内容吗?

谢谢!

APp.js

import React, { Component } from 'react';
import Form from './components/Form';
import Results from './components/Results';

import './App.css';

class App extends Component {
  state = {
    title: '',
    author: '',
    isbn: ''
  }

  render() {
    //const {title, author, isbn} = this.state;
    return (
      <React.Fragment>
        <div className="container">
          <Form />
          <Results 
            title={this.state.title}
            author={this.state.author}
            isbn={this.state.isbn}
          />
        </div>
      </React.Fragment>
    );
  }
}

export default App;

Form.js:

import React, { Component } from 'react';


class Form extends Component {
    constructor(props){
        super(props);
        this.state = {
            title: '',
            author: '',
            isbn: '',
        }

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

    handleSubmit = (e) => {
        e.preventDefault();
        //const {title, author, isbn} = this.state;
        console.log('Title: ' + this.state.title);
        console.log('Author: ' + this.state.author);
        console.log('ISBN: ' + this.state.isbn);

    }

    onChangeHandler = (e) => {
        //const {title, author, isbn} = e.target;
        this.setState({
            [e.target.name]: e.target.value
        })   

    }

  render() {
    return (
      <div>
        <div className="container mt-4">
            <h1 className="display-4 text-center">
                My<span className="text-primary">Book</span>List
            </h1>
        <form onSubmit={this.handleSubmit}>
            <div className="form-group">
                <label htmlFor="title">Title</label>
                <input 
                    type="text" 
                    className="form-control"
                    name="title" 
                    value={this.state.title}
                    onChange={this.onChangeHandler} />
            </div>
            <div className="form-group">
                <label htmlFor="author">Author</label>
                <input 
                    type="text"  
                    className="form-control"
                    name="author"
                    value={this.state.name}
                    onChange={this.onChangeHandler} />
            </div>
            <div className="form-group">
                <label htmlFor="isbn">ISBN#</label>
                <input 
                    type="text" 
                    className="form-control"
                    name="isbn"
                    value={this.state.name}
                    onChange={this.onChangeHandler} />
            </div>
                <input
                    type="submit"
                    value="Add Book"
                    className="btn btn-primary btn-block"
                />
        </form>

        </div>

      </div>


    )
  }
}


export default Form;

Results.js

import React from 'react';

const Results = (props) => {
    console.log(props);
    return ( 
        <div>
            <h3>Results</h3>
            <p>{props.title}</p>
            <p>{props.author}</p>
            <p>{props.isbn}</p>
        </div>

     );
}

export default Results;

1 个答案:

答案 0 :(得分:1)

您需要将Form的值传递给它的父级,以便父级可以设置Result属性,这是您可以执行的操作。

class App extends Component {
  ....
  onFormSubmit = (author, title, isbn) => {
    this.setState({title, author, isbn});
  }

  render() {
    return (
      ....
      <Form onFormSubmit={this.onFormSubmit}/>
      ....  
    );
  }
}


class Form extends Component {
    ....

    handleSubmit = (e) => {
        e.preventDefault();
        const { author, title, isbn } = this.state;   
        this.props.onFormSubmit(author, title, isbn);
    }
    ....
}