简体   繁体   English

react-native将两个数组合并为一个

[英]react-native combining two arrays into one

I am trying to let users select their interest (ie, food, sports) and then display it on users profile page. 我试图让用户选择他们的兴趣(例如食物,运动),然后将其显示在用户个人资料页面上。

I have two separate views. 我有两种不同的看法。 One is where users select their favorite food and in the next page user could select their favorite sports. 一种是用户选择自己喜欢的食物,然后在下一页中用户可以选择自己喜欢的运动。

So for example, I want to show Interest: pizza, hotdog, basketball, surfing on screen. 例如,我想展示“ Interest: pizza, hotdog, basketball, surfing在屏幕上Interest: pizza, hotdog, basketball, surfing

The problem I am having is that I can't merge two arrays(food and sports). 我遇到的问题是我无法合并两个数组(食物和运动)。

class UserInterest extends Component {
    constructor(props){
       super(props)
       this._favoriteFood = this._favoriteFood.bind(this)
       this._favoriteSports = this._favoriteSports.bind(this)

     this.state = {
        food:[],
        sports:[],
        interest:[],   
     }
    } 

     _favoriteFood(foodState){
        const food = foodState
        this.setState({toggle1:food})
        console.log(food)
        console.log(this.state.food)
      }

     _favoriteSports(SportsState){
        const sports = SportsState
        this.setState({toggle2:sports})
        console.log(sports)
        console.log(this.state.sports)
      }

render(){
 const {toggleText1} =this.state
 const {toggleText2} =this.state

return (
<View>
      <FavoriteFood favoriteFood={this._favoriteFood}/>
</View>
       <View>
             <FavoriteSports favoriteSports={this._favoriteSports}/>
       </View>
       <View>
             <Text>{toggleText1}</Text>
       </View>
               <View>
                     <Text>{toggleText2}</Text>
               </Vie>
)
}

If I have two arrays food:[pizza, hotdog] and sports:[basketball, surfing] How could I merge these two arrays and put it in interest:[] array? 如果我有两个数组, food:[pizza, hotdog]sports:[basketball, surfing]我如何合并这两个数组并将其放入interest:[]数组?

Also when I console.log(food) I get array data, but when I console.log(this.state.food) I get an empty array. 另外,当我console.log(food)我得到数组数据,但是当我console.log(this.state.food)我得到一个空数组。 Shouldn't these be the same? 这些不应该一样吗? I am confused. 我很困惑。

Any advise or comments would be really appreciated thanks in advance! 任何建议或意见将不胜感激,谢谢!

You see the empty array, because your function lost your context, when your call it from another component and this contains these components (FavoriteSports, FavoriteFood etc.). 您会看到一个空数组,因为当您从另一个组件调用该函数时, this函数丢失了上下文,并且其中包含这些组件(FavoriteSports,FavoriteFood等)。 So you need to consolidate context of UserInterest component. 因此,您需要整合UserInterest组件的上下文。 You can use arrow function for it 您可以使用箭头功能

  _favoriteSports = (SportsState) => {
    const sports = SportsState
    this.setState({toggle2:sports})
    console.log(sports)
    console.log(this.state.sports)
  }

For merge arrays you can use es6 spread operator 对于合并数组,您可以使用es6扩展运算符

const interest = [...food, ...sports];

You can use array concat 您可以使用数组concat

function concatArray(){
  let food=this.state.food;
  let sports=this.state.sports;
  let interest=[];
  interest.concat(food);
  interest.concat(sports);
  this.setState({interest:interest});
}

For your second question, 关于第二个问题,

You are passing parameters to the function and changing the state to toggle1 and toggle2. 您正在将参数传递给函数,并将状态更改为toggle1和toggle2。 Change the parameters or predefine the state in the constructor. 更改参数或在构造函数中预定义状态。

I also want to clarify this will change the value of the state. 我还想澄清一下这将改变状态的值。 But console.log(this.state.sports) will not log the updated value because the changes will be affected after one clock cycle and the console.log will occur before that. 但是console.log(this.state.sports)不会记录更新的值,因为更改将在一个时钟周期后受到影响,而console.log将在此之前发生。

_favoriteFood(foodState){
  const food = foodState
  this.setState({food :food})
  console.log(food)
  console.log(this.state.food)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM