首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法将道具从父组件传递给子组件

无法将道具从父组件传递给子组件
EN

Stack Overflow用户
提问于 2019-08-20 20:48:54
回答 1查看 190关注 0票数 2

我的App.js中有导航屏幕,其中一个屏幕将自定义标题呈现为:

代码语言:javascript
运行
复制
const DailyStack = createStackNavigator({
  Dashboard,
  SalesDashboard: {
    screen : SalesDashboard,
    navigationOptions:{
      header: null,
    }
  },
  StackNavSecond : {
    screen: StackNavSecond, 
      navigationOptions : {
        header : <CustomHeader />,

      }
  },....

然后在我的CustomHeader.js文件中,我有一些状态数据:

代码语言:javascript
运行
复制
class CustomHeader extends Component {

  constructor(props) {
    super(props)

    this.state = {
      title:'Regions',
      subtitle:'',
      oem: ''
    }
  }

 async componentDidMount(){

   let car_brand = await AsyncStorage.getItem('brand_name');
   let main_oem = await AsyncStorage.getItem('oem_name');

   await this.setState({
             oem: main_oem,
             subtitle: car_brand,
   });

   console.log(this.state.oem)
  }



render() {
    console.log(this.state.title)
   const {title, subtitle, oem} = this.state;
    return (
             <View>  
               <CustomDropdown title={title} subtitle={subtitle} oem={oem} /> 
             </View>
          )
     }
}

export default withNavigation(CustomHeader);

prop title不会被传递给它的子组件,它将在另外两个屏幕中进一步呈现。

CustomDropdown.js的代码是:

代码语言:javascript
运行
复制
    class CustomDropdown extends Component {

        constructor(props) {
            super(props)

            this.state = {
                 title: '',
                 oem: '',
                 subtitle:''
            };
        }

        componentDidMount(){
           this.setState({
             title:this.props.title,
             subtitle: this.props.subtitle,
             oem: this.props.oem, 
           });
           console.log(this.state.title, this.state.oem, this.state.subtitle)
        }

        render() {
             return (
                <View style={{flexDirection:'row', justifyContent: 'space-between'}}>
                  .........  
                </View>
            )
        }
    }


export default withNavigation(CustomDropdown);

当我对this.state.title进行控制台操作时,它打印出subtitleoem的值。

请帮助解决此问题。

EN

回答 1

Stack Overflow用户

发布于 2019-08-20 21:16:27

当您不想交付组件时,可以使用withNavigation。但是你的代码嵌套得很深。

如果你想像这样使用它,你可以像这样修改代码。

CustomHeader.js

代码语言:javascript
运行
复制
class CustomHeader extends React.Component {

  constructor(props) {
    super(props)

    this.state = {
      title:'Regions',
      subtitle:'',
      oem: ''
    }
  }

 async componentDidMount(){

   let car_brand = await AsyncStorage.getItem('brand_name');
   let main_oem = await AsyncStorage.getItem('oem_name');

   this.setState({
             oem: main_oem,
             subtitle: car_brand,
   });

   console.log(this.state.oem)
  }


render() {
    console.log(this.state.title)
   const {title, subtitle, oem} = this.state;
    return (
             <View>  
               <CustomDropdown title={title} subtitle={subtitle} oem={oem} /> 
             </View>
          )
     }
}

export default withNavigation(CustomHeader);

CustomDropdown.js

代码语言:javascript
运行
复制
    class CustomDropdown extends React.Component {

        constructor(props) {
            super(props);

            this.state = {
                 title: props.title,
                 oem: props.oem,
                 subtitle: props.subtitle
            };
        }

        componentDidMount(){
           console.log(this.state.title, this.state.oem, this.state.subtitle)
        }

        render() {
             return (
                <View style={{flexDirection:'row', justifyContent: 'space-between'}}>
                  .........  
                </View>
            )
        }
    }


export default CustomDropdown;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57574076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档