如何在反应js中将输入值从一个页面发送到另一个页面

时间:2017-03-15 10:32:07

标签: javascript reactjs

import React, { Component } from 'react';
import { Link } from 'react-router'
import SocialButtons from '../SocialButtons/'

class Modals extends Component {
  constructor(props){
    super(props);
    this.state = {inputuuid: '',
    inputmajor: '' ,
    inputminor: '' ,
    inputmanufacturer: ''};
    this.handleUuid = this.handleUuid.bind(this);
    this.handleMajor = this.handleMajor.bind(this);
    this.handleMinor = this.handleMinor.bind(this);
    this.handleManufacturer = this.handleManufacturer.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    alert("started");

}

handleUuid(event) {

    this.setState({inputuuid: event.target.value});
  }
  handleMajor(event){
    this.setState({inputmajor: event.target.value});
  }
  handleMinor(event){
    this.setState({inputminor: event.target.value});
  }
  handleManufacturer(event){
        this.setState({inputmanufacturer: event.target.value});
  }
handleSubmit(event) {
  fetch("http://api-env.bt2qjip33m.ap-south-1.elasticbeanstalk.com/api/v1/beacons" ,
 {
  method: 'post',
  headers: {
    'Content-Type': 'application/json',
    'Authorization' : 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1OGMyOTdiOWQzZWM4NjY4MDMwNDBlNjgiLCJlbWFpbCI6ImtrQGxpdGlmZXIuY29tIiwiZmlyc3ROYW1lIjoiS2lzaGxheSIsImxhc3ROYW1lIjoiS2lzaG9yZSIsImlhdCI6MTQ4OTE0NzgzM30.nHW5w3SSov8ySziblmw2VNlGI3CsZFR-v41Jeg9uBVs'
},
body: JSON.stringify({
    name: "beacon name 1234",
    description: "beacon description here for beacon",
    uuid:this.state.inputuuid,
    major: this.state.inputmajor,
    minor:this.state.inputminor,
    manufacturer:this.state.inputmanufacturer,
    beaconType: "type9",
    location: "main gate8",
    floor: "ist",
    zone: "58c29c06d3ec866803040e6e"
})
}).then(function(response){
  if(response.ok) {
      console.log(response)
    return response;
  }
  throw new Error('Network response was not ok.');
}).then(function(data) { 
  console.log(data);
})
.catch(function(error) {
  console.log('There has been a problem with your fetch operation: ' + error);
});

    alert('Submitted: ' + this.state.inputuuid + this.state.inputmajor + this.state.inputminor + this.state.inputmanufacturer);
    event.preventDefault();
  }

  render() {
    
    return (<div><div>
          <div className="animated fadeIn">
            <br /><div className="card" style={{ width: 57 + '%' }}>
              <div className="card-header">
                 Beacon Settings
              </div>
              <div className="card-block">
                <div className="input-group mb-1">
                  <span className="input-group-addon"><i className="icon-user"></i></span>
                  <input type="text"  className="form-control" value={this.state.inputuuid} onChange={this.handleUuid} placeholder="UUID" />
                </div>

                <div className="input-group mb-1">
                  <span className="input-group-addon"><i className="fa fa-align-justify"></i></span>
                  <input type="text" className="form-control" value={this.state.inputmajor} onChange={this.handleMajor} placeholder="Major Number"/>
                </div>

                <div className="input-group mb-1">
                  <span className="input-group-addon"><i className="fa fa-align-justify"></i></span>
                  <input type="text" className="form-control" value={this.state.inputminor} onChange={this.handleMinor} placeholder="Minor Number"/>
                </div>

                <div className="input-group mb-1">
                  <span className="input-group-addon"><i className="fa fa-align-justify"></i></span>
                  <input type="text" className="form-control" value={this.state.inputmanufacturer} onChange={this.handleManufacturer} placeholder="Manufacturer Number"/>
                </div><center>
                <Link to={'/components/tabs'} style={{ width: 27 + '%' }} className="nav-link btn btn-block btn-success"  onClick={this.handleSubmit} activeClassName="active">Save</Link>

<Link to={'/components/tabs'} style={{ width: 27 + '%' }} className="nav-link btn btn-block btn-success" activeClassName="active">Advance Settings</Link>
</center>

            </div>
          </div>
</div></div>
      </div>
      
        
      
    )
  }
}

export default Modals;

import React, { Component } from 'react';
import { Link } from 'react-router'
import { Dropdown, DropdownMenu, DropdownItem, Progress } from 'reactstrap';
import Modals from '../Modals';
class SocialButtons extends Component {
  constructor(props) {
    super(props);
    this.state = {inputname: '',
    inputdescription: '' ,
    inputzone: '' ,
    inputstore: '' ,
    inputfloor: ''};
    this.handleName = this.handleName.bind(this);
    this.handleDescription = this.handleDescription.bind(this);
    this.handleZone = this.handleZone.bind(this);
    this.handleStore = this.handleStore.bind(this);
    this.handleFloor = this.handleFloor.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    alert("hey");
    this.toggleStore = this.toggleStore.bind(this);
    this.toggleFloor = this.toggleFloor.bind(this);
    this.toggleZone = this.toggleZone.bind(this);
    this.state = {
      dropdownZone: false
    };
    this.state ={
      dropdownStore: false
    };
    this.state ={
      dropdownFloor: false
    };
    this.state = {
            data:[
                {name:"Nikko Laus1"},
                {name:"Sam Collins"},
                {name:"Carl Smith Wesson"}
            ],
            store:[
                {name:"abcd"},
                {name:"Sam Collins"},
                {name:"Carl Smith Wesson"}   
            ],
            floor:[
                {name:"Ist"},
                {name:"IInd"},
                {name:"IIIrd"}   
            ]
        };

  }

  toggleZone() {
    this.setState({
      dropdownZone: !this.state.dropdownZone
    });
  }
  toggleStore() {
    this.setState({
      dropdownStore: !this.state.dropdownStore
    });
  }
  toggleFloor() {
    this.setState({
      dropdownFloor: !this.state.dropdownFloor
    });
  }

  handleName(event) {

    this.setState({inputname: event.target.value});
  }
  handleDescription(event){
    this.setState({inputdescription: event.target.value});
  }
  handleZone(event){
    this.setState({inputzone: event.target.value});
  }
  handleFloor(event){
        this.setState({inputfloor: event.target.value});
  }
  handleStore(event){
        this.setState({inputstore: event.target.value});
  }
  handleSubmit(event){

    alert('Submitted: ' + this.state.inputname + this.state.inputdescription + this.state.inputzone + this.state.inputstore + this.state.inputfloor);
    event.preventDefault();
  }


  render() {
    let Zone = this.state.data.map(person =>
        {
            return <DropElement key={person.id} data={person}/>
        });

    let Store = this.state.store.map(person =>
    {
        return <DropElement key={person.id} data={person}/>
    });
    let Floor = this.state.floor.map(person =>
    {
        return <DropElement key={person.id} data={person}/>
    });
    return (<div><div>
          <div className="animated fadeIn">
            <br /><div className="card" style={{ width: 77 + '%' }}>
              <div className="card-header">
                 Create Beacon
              </div>
              <div className="card-block">
                <div className="input-group mb-1">
                  <span className="input-group-addon"><i className="icon-user"></i></span>
                  <input type="text" className="form-control" value={this.state.inputname} onChange={this.handleName} placeholder="Name" />
                </div>

                <div className="input-group mb-1">
                  <span className="input-group-addon"><i className="fa fa-align-justify"></i></span>
                  <input type="textArea" value={this.state.inputdescription} onChange={this.handleDescription} className="form-control" placeholder="Description"/>
                </div>
                <div className="card-header">
                Select a zone</div>
                <div className="card-block"><div className="px-1 row">Zone:
                <div className="px-2 mb-1">
                
                <Dropdown isOpen={this.state.dropdownZone}  toggle={this.toggleZone}>
                    <input type="text" value={this.state.inputzone} onChange={this.handleZone} className="caret" placeholder="Select a Zone" 
                    onClick={this.toggleZone} data-toggle="dropdown" aria-haspopup="true" aria-expanded={this.state.dropdownZone} />
                     
                    <DropdownMenu>

                      <DropdownItem>{Zone}<divider /></DropdownItem>
                      
                    </DropdownMenu>
                  </Dropdown></div></div></div>

                  <div className="card-header">
                Select a Store</div>
                <div className="card-block"><div className="px-1 row">Store:
                <div className=" px-2 mb-1">
                
                  <Dropdown isOpen={this.state.dropdownStore}  toggle={this.toggleStore}>
                    <input type="text" value={this.state.inputstore} onChange={this.handleStore}  className="caret" placeholder="Select a Store" 
                    onClick={this.toggleStore} data-toggle="dropdown" aria-haspopup="true" aria-expanded={this.state.dropdownStore} />
                     
                    <DropdownMenu>

                      <DropdownItem>{Store}<divider /></DropdownItem>
                      
                    </DropdownMenu>
                  </Dropdown>

                </div></div></div>
                  <div className="card-header">
                Select a floor</div>
                <div className="card-block"><div className="px-1 row">Floor:
                <div className=" px-2 mb-1">

                  <Dropdown isOpen={this.state.dropdownFloor}  toggled={this.toggleFloor}>
                    <input type="text" value={this.state.inputfloor} onChange={this.handleFloor}  placeholder="Select a Floor" 
                    onClick={this.toggleFloor} data-toggled="dropdown" aria-haspopup="true" aria-expanded={this.state.dropdownFloor} />
                     
                    <DropdownMenu>

                      <DropdownItem>{Floor}<divider /></DropdownItem>
                     
                    </DropdownMenu>
                  </Dropdown></div></div></div>


                  </div>
                
              </div>
              
              </div>
            </div>
          
          <div className="px-2 row" style={{ width: 90 + '%' }}>
          <div><Link to={'/components/Modals'}  className="nav-link btn btn-block btn-success" activeClassName="active">
                Next</Link>
                </div><div className="px-2"><Link to={'/components/social-buttons'} className="nav-link btn btn-block btn-danger" activeClassName="active"> Cancel</Link>
</div>
<div className="float-right"><Link to={'/components/social-buttons'} className="nav-link btn btn-block btn-success" activeClassName="active">
Default Notification
</Link></div>
</div></div>
      
      
        
      
    )
  }
}

class DropElement extends React.Component
{
    constructor()
    {
        super();
    }

    render()
    {
        return (<DropdownItem>{this.props.data.name}
                   </DropdownItem>
            );
    }
}

export default SocialButtons;

我添加了2个代码段 我想将名称描述作为来自socialbuttons.js的输入,然后单击下一步按钮,这些值将在另一个modals.js页面上发送,然后使用POST请求将这些值发送到服务器。

0 个答案:

没有答案