亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

undefined 不是一個對象(評估'_this.state')

undefined 不是一個對象(評估'_this.state')

偶然的你 2023-05-11 16:08:25
嘗試訪問 this.state 時,我一直收到此錯誤 到目前為止,我嘗試過的所有解決方案均無效這是我的構造函數export default class Layout extends React.Component {    constructor() {        super();        this.state = {            bagTitle: 'View Bag'        }        this.renderContent = this.renderContent.bind(this);    }    render() {        return (            <NavigationContainer>                <Stack.Navigator>                    <Stack.Screen name='Menu' component={HomeScreen}                        options={{                            headerRight: () => (                                <Ionicons style={iconView.icons} name='ios-list' size={26} color="black" />                            ),                        }}                    />                </Stack.Navigator>            </NavigationContainer>        );    };}這是我調用 renderContent 函數的地方class HomeScreen extends React.Component {  render() {    const title = 'View Bag'    return (      <View style={styles.container}>        <StatusBar style="auto" />        {/* Bottomsheet View */}        <BottomSheet          ref={sheetRef}          snapPoints={[100, 300, Dimensions.get('window').height - 100]}          borderRadius={10}          renderContent={renderContent}        >          <Text onPress={showBag} style={{ backgroundColor: 'red', fontSize: 17, fontWeight: '600', textAlign: 'center', position: "absolute", marginTop: 20, width: windowWidth }}>View Bag</Text>        </BottomSheet>      </View>    );  }}這是我試圖訪問 this.state 的地方renderContent = () => (    <View      style={{        backgroundColor: '#ecf0f1',        padding: 16,        fontSize: 20,        height: windowHeight,        width: windowWidth,        marginBottom: -200      }}    >      <Text onPress={showBag} style={{ fontSize: 17, fontWeight: '600', textAlign: 'center', position: "absolute", marginTop: 20, width: windowWidth }}>{this.state.bagTitle}</Text>    </View>);繼續收到此錯誤undefined 不是一個對象(評估'_this.state')
查看完整描述

1 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

您可以在課堂上使用showBag和renderContentHomeScreen


class HomeScreen extends React.Component {

  constructor() {

    super();

    this.state = {

      bagTitle: 'View Bag'

    }

    this.showBag = this.showBag.bind(this);

    this.renderContent = this.renderContent.bind(this);

  }


  // FUNCTIONS

  showBag = (item) => {

    sheetRef.current.snapTo(1);

    if (item.title !== null) {

      this.setState({

        bagTitle: item.title

      })

      console.log('ITEM: ', item);

    }

  }


  renderContent = () => (

    <View

      style={{

        backgroundColor: '#ecf0f1',

        padding: 16,

        fontSize: 20,

        height: windowHeight,

        width: windowWidth,

        marginBottom: -200

      }}

    >

      <Text onPress={this.showBag} style={{ fontSize: 17, fontWeight: '600', textAlign: 'center', position: "absolute", marginTop: 20, width: windowWidth }}>{this.state.bagTitle}</Text>

    </View>

  );


  render() {

    const title = 'View Bag'

    return (

      <View style={styles.container}>

        <StatusBar style="auto" />

        {/* Bottomsheet View */}


        <BottomSheet

          ref={sheetRef}

          snapPoints={[100, 300, Dimensions.get('window').height - 100]}

          borderRadius={10}

          renderContent={this.renderContent}

        />

      </View>

    );

  }

}


查看完整回答
反對 回復 2023-05-11
  • 1 回答
  • 0 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號