简体   繁体   English

React Native-将数据从组件传递到另一个

[英]React Native - Passing data from a component to another

I took over a React Native project from somebody else, this project is quite old (2 years) and I came across the following: 我从其他人那里接手了一个React Native项目,这个项目已经很老了(2年),遇到了以下问题:

Home.js Component: (I simplified it) Home.js组件:(我简化了)

export let customersData = null;

export default class Home extends Component {  

    render() {
        return (
            <JumboButton
                onPress={() => {
                  this.props.navigator.push({
                    component: CustomerSearch
                  });
                }}
              >
        );
    }

    _getAllCustomers(limit, sortAttr, order) {
        apiCall.(apiUrl, {
        ...
        }).then((responseData) => {
            const customersDataAll = responseData.data;
            customersData = customersDataAll.filter((f) => {
                return f.lastname !== ''
            });
        });
    }
}

So within the Home-Component, customersData is filled with data. 因此,在家庭组件中,customersData充满了数据。 Also the component CustomerSearch is called and within CustomerSearch I found this: 组件CustomerSearch也被调用,并且在CustomerSearch中我发现了这一点:

CustomerSearch.js: CustomerSearch.js:

import {customersData} from './Home';

export default class CustomerSearch extends Component {
    constructor(props) {
        super(props);
        this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
        this.state = {
            dataSource: this.ds.cloneWithRows(customersData),
        };
    }
}

Two things are a bit weird for me: 两件事对我来说有点奇怪:

  1. Is it correct to set customersData and not this.customersData inside the callback of the api call? 在api调用的回调中设置customersData而不是this.customersData是否正确?

  2. Currently I get this error https://d.pr/i/IUzxdf "Cannot convert undefined or null to object" and I assume that this is because of the data import of customersData in CustomerSearch.js. 当前,我收到此错误https://d.pr/i/IUzxdf “无法将未定义的或null转换为对象”,并且我认为这是由于CustomerSearch.js中的customersData数据导入。 Is the place where I need to look? 是我需要看的地方吗? Btw is there any chance that react tells me the exact line and file where this error occurs? 顺便说一句,有什么反应可以告诉我发生此错误的确切行和文件吗?

Thanks for your help! 谢谢你的帮助!

  1. Short answer is that it is definitely an unusual React pattern, and not one that many people would recommend. 简短的回答是,这绝对是一种不常见的React模式,并不是很多人会推荐的模式。 Importing a let variable into another file is not a reliable way to share information between components. let变量导入另一个文件不是在组件之间共享信息的可靠方法。

It would be far more sensible to attach customersData to your parent component's state and pass it to CustomersSearch through a prop - ie customersData附加到父组件的state并将其通过prop传递给CustomersSearch会更明智。

export default class Home extends Component {
  constructor (props) {
     super(props);
     this.state = { customersData: null };
     this._getAllCustomers = this._getAllCustomers.bind(this)
  }

  render() {
    return (
        <JumboButton
          onPress={() => {
            this.props.navigator.push({
              component: props =>
                <CustomerSearch customersData={this.state.customersData} {...props} />
            });
          }}
        >
    );
  }

  _getAllCustomers(limit, sortAttr, order) {
    apiCall.(apiUrl, {
    ...
    }).then((responseData) => {
        const customersDataAll = responseData.data;
        const customersData = customersDataAll.filter((f) => {
            return f.lastname !== ''
        });
        this.setState({ customersData });
    });
  }
}

Not sure how your JumboButton 's onPress prop works exactly, but you should get the idea? 不确定JumboButtononPress道具如何正常工作,但是您应该明白吗?

And in answer to 2. - Yes I would imagine this is the problem! 并回答2。-是的,我想这是问题所在!

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

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