我从父母那里调用了两个相同的子组件。一个工作正常,其他人没有渲染,甚至没有调用我通过控制台检查put put。 这是我父组件的代码 我在我的代码中调用NearbyList组件(它是父组件,我在这里调用我的子组件,你可以看到NearByList只有第一个是调用而不是下一个)
import NearByList from "../../Uicomponents/NearbyLists/NearbyList"
class RestaurantList extends Component {
constructor(props) {
super(props);
this.state = {
favRest:null,
selectedRest: null,
NearbyRestaurants: null,
loading: true,
SnacksLoading: this.props.SnacksLoading,
RestaurantLoading: this.props.RestLoading,
fetchRestaurantsError: this.props.fetchRestaurantsError,
fetchSnacksError:this.props.fetchSnacksError
};
this.props.getNearbyrestaurants(50.8507855, 4.3539655);
this.props.getNearbySnacks(50.8507855, 4.3539655);
}
componentWillMount() {
let x= this.props.favList
if (x) {
this.setState({
favRest: x
})
}
}
componentWillReceiveProps(next) {
console.log('sajhdgjahs',next)
if (next.Snacks) {
this.setState({
Snacks: next.Snacks,
SnacksLoading: next.SnacksLoading,
fetchSnacksError: next.fetchSnacksError
});
}
if (next.favList) {
this.setState({ favRest: next.favList });
}
if (next.Nearby) {
this.setState({
NearbyRestaurants: next.Nearby,
RestaurantLoading: next.RestaurantLoading,
fetchRestaurantsError: next.fetchRestaurantsError
});
}
if (next.fetchRestaurantsError) {
this.setState({ fetchRestaurantsError: next.fetchRestaurantsError })
}
}
render() {
return (
<DrawerLayoutAndroid
ref={'DRAWER_REF'}
drawerWidth={250}
drawerPosition={DrawerLayoutAndroid.positions.Left}
renderNavigationView={() => <Sidebar close={()=>
{this.refs['DRAWER_REF'].closeDrawer();}}
navigation={this.props.navigation}/>}>
<View style={{ flex: 1, backgroundColor: this.props.data.theme }}>
<Header menu={()=>this.refs['DRAWER_REF'].openDrawer()} navigation=
{this.props.navigation} title={"Restaurants"} />
<Container style={{backgroundColor:'black'}}>
<Tabs style={{backgroundColor:'black'}}>
<Tab style={{backgroundColor:'black'}}
heading={
<TabHeading style={{backgroundColor:'black'}}>
<Text style={{color:'white'}}>Restaurants</Text>
</TabHeading>}>
<NearByList
FavListofUser={this.state.favRest}
address={'RestaurantDetail'}
navigation={this.props.navigation}
addfav={(id)=>{ this.props.addfav(this.props.User_id,id)}}
removeFav={(id)=>{this.props.removeFav(this.props.User_id,id)}}
loading={this.state.RestaurantLoading}
error={this.state.fetchRestaurantsError}
okok={'jshdjs'}
ListData={this.state.NearbyRestaurants} />
</Tab>
<Tab style={{backgroundColor:'black'}}
heading={
<TabHeading style={{backgroundColor:'black'}}>
<Text style={{color:'white'}}>Snacks</Text>
</TabHeading>}>
<NearByList
FavListofUser={this.state.favRest}
navigation={this.props.navigation}
loading={this.state.SnacksLoading}
address={'SnackDetail'}
addfav={(id)=>{ this.props.addfav(this.props.User_id,id)}}
removeFav={(id)=>{this.props.removeFav(this.props.User_id,id)}}
error={this.state.fetchSnacksError}
ListData={this.state.Snacks} />
</Tab>
</Tabs>
</Container>
</View>
</DrawerLayoutAndroid>
);
}
}
export default connect(
state => ({
data: state.common.get('data'),
Nearby: state.signin.get('NearbyRest'),
Snacks: state.signin.get('Snacks'),
favList: state.common.get('favList'),
User_id: state.signin.get('User_id'),
RestLoading: state.signin.get('RestLoading'),
fetchRestaurantsError: state.signin.get('fetchRestaurantsError'),
SnacksLoading: state.signin.get('SnacksLoading'),
fetchSnacksError: state.signin.get('fetchSnacksError')
}),
dispatch => ({
getData: data => dispatch(Actions.common.getData(data)),
getUserData: data => dispatch(Actions.main.getUserData(data)),
addfav: (user, place) => dispatch(Actions.common.addFavourite(user,
place)),
removeFav: (user, index) => dispatch(Actions.common.removeFavourite(user,
index)),
getNearbyrestaurants: (latitude, longitude) =>
dispatch(Actions.signin.getNearbyrestaurants(latitude, longitude)),
getNearbySnacks: (latitude, longitude) =>
dispatch(Actions.signin.getNearbySnacks(latitude, longitude)),
})
)(RestaurantList);