[英]React Native / Redux - create helper function connected to Redux store in separate file
I'm building an app using React Native & Redux and for a <ListView>
component I'd like to factorise the way in which dataBlob
, sectionIDs
& rowIDs
are created and contain the function within a separate file. 我正在使用React Native和Redux构建一个应用程序,对于一个<ListView>
组件,我想分解dataBlob
, sectionIDs
和rowIDs
的创建方式以及将该函数包含在单独文件中的方式。
Within the separate file I'd like to create a function called formatDataForListView
which is passed an array of IDs, with which the correct parts of the Redux store can be retrieved and the ListView
arrays are constructed accordingly. 在单独的文件中,我想创建一个称为formatDataForListView
的函数,该函数传递一个ID数组,利用该ID数组可以检索Redux存储的正确部分,并相应地构造ListView
数组。
I've tried to contain the function within a React element however because I just want to return (dataBlob, sectionIDs, rowIDs)
, which aren't React elements, I'm getting the error: A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
我试图将函数包含在React元素中,但是因为我只想return (dataBlob, sectionIDs, rowIDs)
不是React元素的return (dataBlob, sectionIDs, rowIDs)
,所以我得到了一个错误: A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
What I've done thus far: 到目前为止,我所做的是:
import { Component } from 'react';
import { connect } from 'react-redux';
class BookingsListViewObjectsCreator extends Component {
render() {
const { bookings } = this.props;
// function that retrieves bookings from Store and structures data
// into something suitable for ListViewWithSections
const formatDataForListView = bookingIDs => {
const dataBlob = {};
const sectionIDs = [];
const rowIDs = [];
// create array of all the different products for grouping into sections
console.log('bookingIDs: ', bookingIDs);
bookingIDs.map((id) => {
// is product already in sectionIDs array? If not, add to sectionIDs & dataBlob
if (sectionIDs.indexOf(bookings[id].product_id) < 0) {
sectionIDs.push(bookings[id].product_id);
dataBlob[bookings[id].product_id] = bookings[id].title;
}
// add booking to rowIDs & dataBlob
rowIDs.push(bookings[id].id);
dataBlob[bookings[id].product_id + ':' + bookings[id].id] = bookings[id];
return id;
});
console.log('dataBlob: ', dataBlob);
console.log('sectionIDs: ', sectionIDs);
console.log('rowIDs: ', rowIDs);
return { dataBlob, sectionIDs, rowIDs };
};
// create objects required to render ListView and return them
const { dataBlob, sectionIDs, rowIDs } = formatDataForListView(this.props.bookingIDs);
return (dataBlob, sectionIDs, rowIDs);
}
}
const mapStateToProps = state => {
return { bookings: state.bookings };
};
export default connect(mapStateToProps)(BookingsListViewObjectsCreator);
So how do I store this straight JS function in a separate file and call it from a React element to retrieve (dataBlob, sectionIDs, rowIDs)
? 那么,如何将这个直接的JS函数存储在单独的文件中,并从React元素中调用它以进行检索(dataBlob, sectionIDs, rowIDs)
?
I think @oldo.nicho means a structure to looks like this. 我认为@ oldo.nicho的意思是这样的结构。 Just export that function from a file and import it into the components that need the functionality 只需从文件中导出该功能并将其导入需要该功能的组件中
// somefile.js // somefile.js
// function that retrieves bookings from Store and structures data
// into something suitable for ListViewWithSections
export default function formatDataForListView = bookingIDs => {
const dataBlob = {};
const sectionIDs = [];
const rowIDs = [];
// create array of all the different products for grouping into sections
bookingIDs.forEach((id) => {
// is product already in sectionIDs array? If not, add to sectionIDs & dataBlob
if (sectionIDs.indexOf(bookings[id].product_id) < 0) {
sectionIDs.push(bookings[id].product_id);
dataBlob[bookings[id].product_id] = bookings[id].title;
}
// add booking to rowIDs & dataBlob
rowIDs.push(bookings[id].id);
dataBlob[bookings[id].product_id + ':' + bookings[id].id] = bookings[id];
});
return { dataBlob, sectionIDs, rowIDs };
};
// otherfile.js // otherfile.js
import { Component } from 'react';
import { connect } from 'react-redux';
import formatDataForListView from './somefile';
class BookingsListViewObjectsCreator extends Component {
render() {
const { bookings } = this.props;
// create objects required to render ListView and return them
const { dataBlob, sectionIDs, rowIDs } = formatDataForListView(this.props.bookingIDs);
return (dataBlob, sectionIDs, rowIDs);
}
}
const mapStateToProps = state => {
return { bookings: state.bookings };
};
export default connect(mapStateToProps)(BookingsListViewObjectsCreator);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.