简体   繁体   English

React Native-如何使用组件作为JavaScript对象?

[英]React Native - How to use a component as a JavaScript object?

I'm newbie to React Native and having this problem when I was trying to build an iPhone application. 我是React Native的新手,在尝试构建iPhone应用程序时遇到了这个问题。

My code is like: 我的代码是这样的:

var React = require('react-native');
var MyListView = require('./MyListView');  // which renders a <ListView>...</ListView>

var SearchPage = React.createClass({
  onSubmitEditing: function () { /* do something */ },
  render: function() {
    return (
      <ScrollView>
        <TextInput onSubmitEditing={this.onSubmitEditing} />
        <MyListView />
      </ScrollView>
    )
  },
})

And I need to change the state of MyListView (update data source ), when searching ( onSubmitEditing event). 我需要在搜索( onSubmitEditing事件)时更改MyListView (更新数据源 )的状态 How should I do that? 我应该怎么做? I thought there may be some solutions if I can use <MyListView /> as a JavaScript object, which I don't know how. 我认为如果可以将<MyListView />用作JavaScript对象,可能会有一些解决方案,但我不知道如何做。

Any helps will be appreciate! 任何帮助将不胜感激! Thanks in advance! 提前致谢!

You have 2 options: 您有2个选择:

  1. Assuming that in the SearchPage component you know what the data is going to be, you can pass it as a props to the MyListView: ` 假设在SearchPage组件中您知道数据将是什么,则可以将其作为道具传递给MyListView:
  2. However, I think that the better option will be to store the data in a store (flux), and then filter the list in the store. 但是,我认为更好的选择是将数据存储在商店中(通量),然后在商店中过滤列表。 I did something like this: 我做了这样的事情:

    reg = new RegExp(sanitizer.escapeRegExp(query), "i") filteredList = _.filter @initialDataSource(), (item) => item.name.match(reg) this.setState({dataSource: this.state.dataSource.cloneWithRows(filteredList)})

I am using lodash to filter the list and then load the dataSource. 我正在使用lodash筛选列表,然后加载dataSource。 You can pass just the query to the MyListView which is much 'cleaner' <MyListView query={queryValue} /> 您可以仅将查询传递给MyListView,它更“干净” <MyListView query={queryValue} />

I think the best solution will be passing as the props to the MyListView component. 我认为最好的解决方案将作为支持传递给MyListView组件。 You should maintain the state in SearchPage component and in the onSubmitEditing function set the state of it: 您应该在SearchPage组件中维护状态,并在onSubmitEditing函数中设置其状态:

var SearchPage = React.createClass({
getInitialState: function(){
return {toUpdate: false}
}
  onSubmitEditing: function () { this.setState({toUpdate: true})},
  render: function() {
    return (
      <ScrollView>
        <TextInput onSubmitEditing={this.onSubmitEditing} />
        <MyListView toUpdate={this.state.toUpdate}/>
      </ScrollView>
    )
  },
})

Every time setState is called the Search component and its children are re-rendered. 每次setState被称为Search组件时,其子组件都将重新呈现。 Inside the MylistView component you just need to check this.props.toUpdate and perform the operation based upon this prop value. 在MylistView组件内部,您只需要检查this.props.toUpdate并根据此prop值执行操作。

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

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