简体   繁体   English

反应原生中呈现子组件而不是父组件

[英]Render child component and not parent component in react native

The child component not being rendered on a change in parent component 子组件未在父组件的更改中呈现

I have a parent component that has a dropdown. 我有一个父组件有一个下拉列表。 I need to update a child component when the dropdown changes but without rendering the parent component. 我需要在下拉列表更改时更新子组件,但不渲染父组件。

const CrewsAdapter = props => {
  let crewName = 'jack nickelson';
  if(props.data.loading) {
    return <View><Text>Loading crews</Text></View>
  }
  if(props.data.getAllCrews) {
      crewsNames= props.data.getAllCrews.map(crew => ({"value": crew.crewName}));
    return (
          <View style={{ flexDirection: 'column', alignItems: 'center', backgroundColor: '#ffffff' }}>
            <View style={[styles.container, { flex: 0.25 }]}>
              <Text style={styles.headerText}>Crews</Text>
            </View>
            <View style={{ flexDirection: 'row' }}>
              <View style={{ flex: 0.9 }}>
                <Dropdown label="Crews List"
                          data={crewsNames}
                          onChangeText = {(value) => {crewName = value}}
                />
              </View>
            </View>
            <View style={{ flexDirection: 'row' }}>
            <Crew crewName={crewName}/>
            </View>
          </View>
    );
  }
  return <View><Text>No data</Text></View>
};

export default class CrewsScreen extends Component {
  constructor(props) {
    super(props);
    this.stateHandler = this.stateHandler.bind(this);
    this.state = {
      crewName: '',
    }
  }

  stateHandler(value){
    this.setState({crewName: value});
    console.log('crewName: ' + this.state.crewName);
  }

  render() {
    const AllCrews = graphql(crewsList, {options: (props => ({}))})(CrewsAdapter);
    return (
        <SafeAreaView style={styles.screen}>
          <ToolBar />
          <AllCrews stateHandler = {this.stateHandler}/>
          <CrewsListFooter navProps={{...this.props}}/>
        </SafeAreaView>
    );
  }
}

When the value of the "Crews List" changes, I need to re-render the "Crew" component without re-rendering the parent since the dropdown is already generated from an api call. 当“Crews List”的值发生变化时,我需要重新渲染“Crew”组件而不重新渲染父组件,因为已经从api调用生成了下拉列表。

Below is the constructor and componentWillRecieveProps fucntiosn for the Crew component. 下面是Crew组件的构造函数和componentWillRecieveProps fucntiosn。

export default class Crew extends Component {
    constructor(props) {
        super(props);
        this.state = {
            crewName: props.crewName
        }
    }
    componentWillReceiveProps({crewName}) {
        this.setState({...this.state,crewName})
    }

不幸的是,我不认为这是可能的,因为为了更新Crew组件,必须从父 - CrewList向下传递CrewList ,并且在CrewList组件中更改props,从而触发React生命周期的更改

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

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