使用 componentWillUpdate 和 switch 语句



我正在使用React-NativeReact-Native-Firebase,并试图让我的Events组件进行不同的Firebase查询(然后更新redux存储(,具体取决于activityType prop的值。

这是工作正常的父组件。当我更改下拉值并将值传递到<Events />时,它会更新 state.eventType 。

let eventTypes = [{value: 'My Activity'}, {value: 'Friend Activity'}, {value: 'All Activity'}];
state = {
showEventFormModal: false,
eventType: 'Friend Activity'
}
<View style={styles.container}>
<Dropdown
data={eventTypes}
value={this.state.eventType}
containerStyle={{minWidth: 200, marginBottom: 20}}
onChangeText={val => this.setState({eventType: val})}
/>
<Events activityType={this.state.eventType}/>
</View>

这是事件组件。使用Switch语句来确定将哪个 activityType 传递到 props 中。我遇到的问题是无限循环,因为在每个 case 语句中,我正在调度操作来更新存储,这会导致重新渲染和组件 WillUpdate(( 重新触发。我试图理解的是处理这个问题的最佳方法是什么?因为很明显我的方法现在无法正常工作。是否有一种共同的反应模式来实现这一点?

// GOAL: when this components props are updated
// update redux store via this.props.dispatch(updateEvents(events))
// depending on which type of activity was selected
componentWillUpdate() { 
let events = [];
switch(this.props.activityType) {
case 'Friend Activity': // get events collections where the participants contains a friend
// first get current users' friend list
firebase.firestore().doc(`users/${this.props.currentUser.uid}`)
.get()
.then(doc => {
return doc.data().friends
})
// then search the participants sub collection of the event
.then(friends => {
firebase.firestore().collection('events')
.get()
.then(eventsSnapshot => {
eventsSnapshot.forEach(doc => {
const { type, date, event_author, comment } = doc.data();
let event = {
doc, 
id: doc.id,
type,
event_author,
participants: [],
date,
comment,
}
firebase.firestore().collection('events').doc(doc.id).collection('participants')
.get()
.then(participantsSnapshot => {
for(let i=0; i<participantsSnapshot.size;i++) {
if(participantsSnapshot.docs[i].exists) {
// if participant uid is in friends array, add event to events array
if(friends.includes(participantsSnapshot.docs[i].data().uid)) {
// add participant to event
let { displayName, uid } = participantsSnapshot.docs[i].data();
let participant = { displayName, uid }
event['participants'].push(participant)
events.push(event)
break;
}
}
}
})
.then(() => {
console.log(events)
this.props.dispatch(updateEvents(events))
})
.catch(e => {console.error(e)})
})
})
.catch(e => {console.error(e)})
})
case 'My Activity': // get events collections where event_author is the user
let counter = 0;
firebase.firestore().collection('events').where("event_author", "==", this.props.currentUser.displayName) 
.get()
.then(eventsSnapshot => {
eventsSnapshot.forEach(doc => {
const { type, date, event_author, comment } = doc.data();
let event = {
doc, 
id: doc.id,
type,
event_author,
participants: [],
date,
comment,
} 
// add participants sub collection to event object
firebase.firestore().collection('events').doc(event.id).collection('participants')
.get()
.then(participantsSnapshot => {
participantsSnapshot.forEach(doc => {
if(doc.exists) {
// add participant to event
let { displayName, uid } = doc.data();
let participant = { displayName, uid }
event['participants'].push(participant)
}
})
events.push(event);
counter++;
return counter;
})
.then((counter) => {
// if all events have been retrieved, call updateEvents(events)
if(counter === eventsSnapshot.size) {
this.props.dispatch(updateEvents(events))
}
})
})
})
case 'All Activity':
// TODO
// get events collections where event_author is the user 
// OR a friend is a participant
}
}

更新存储最好对用户操作执行。因此,我会在DropdownonChange 事件中更新存储,而不是在组件 WillUpdate 函数中更新存储。

在发现我可以通过componentDidUpdate访问prevProps后,我已经找到了处理这个问题的干净方法。这样,我可以将以前的活动类型与当前活动类型进行比较,如果它们已更改,则componentDidUpdate它应该调用fetchData(activityType)

class Events extends React.Component {
componentDidMount() {
// for initial load 
this.fetchData('My Activity')
}
componentDidUpdate(prevProps) {     
if(this.props.activityType !== prevProps.activityType) {
console.log(`prev and current activityType are NOT equal. Fetching data for ${this.props.activityType}`)
this.fetchData(this.props.activityType)
}
} 
fetchData = (activityType) => {
//switch statements deciding which query to perform
...
//this.props.dispatch(updateEvents(events))
}
}

https://reactjs.org/docs/react-component.html#componentdidupdate

相关内容

  • 没有找到相关文章

最新更新