想在reactjs中将状态存储在数组中

时间:2018-03-14 10:51:01

标签: reactjs

嘿伙计我有一个状态我想将它存储在数组中我怎么能这样做?而我想传递它作为另一个组件中的道具可以传递数组中的道具?

或者我是否必须将此状态存储在我传递给它的数组中?

请告诉我。

这是我的代码:

import React, { Component } from 'react'

export default class Join extends Component {
constructor(props)
{
super(props)
this.state={
  Rid:'',
  Rname:'',
  Rgender:''
}
this.onChange=this.onChange.bind(this);
this.register=this.register.bind(this);

}
onChange(e)
{
this.setState({[e.target.name]:e.target.value});
console.log(this.state);
}
register(e)
{
e.preventDefault();
this.props.join(this.state.Rid,this.state.Rname,this.state.Rgender);

console.log('ready to go');
}
 render() {
 return (
    <div className="login">

   <form  >
   <img src={require("../images/logo.png")} style=
   {{maxWidth:'80vh',maxHeight:'100vh'}}alt={''}/> 


   <div>
       <label>Your ID:</label>
       <input type="text" name="Rid" onChange=
     {this.onChange}placeholder="Enter your ID"/>

   </div>
   <br/>
   <div>
       <label>Your Name:</label>
       <input type="text" name="Rname"onChange={this.onChange} 
       placeholder="Enter your Name"/>
   </div>


   

<label>Your Gender:</label>
<label>Female:</label>
<input type="radio" name="Rgender" value="Female" onChange=
   {this.onChange}/> 

       <label>Male:</label>
       <input type="radio" name="Rgender" value="Male" onChange={this.onChange}/> 

 </div>
 <button type="button" className="btn btn-info" onClick={this.register}>Register</button> 


  </form>

  </div>

我将它传递给另一个组件。我想将它存储在数组中。

1 个答案:

答案 0 :(得分:0)

创建一个新数组,将数据放入IN并将数据传递给另一个组件:

let newArray = [];
newArray.push(this.state);

<OtherComponent data={newArray} />