代码之家  ›  专栏  ›  技术社区  ›  Norfeldt

本地响应:组件重新发送程序,但属性未更改

  •  0
  • Norfeldt  · 技术社区  · 8 年前

    我遇到了一个奇怪的问题,我可以弄明白为什么会发生这种事。

    这不应该发生,因为道具被传递到 history. 组件尚未更新。

    < Buff行情>

    /components/history.js

    < /块引用> <预> <代码>… 常量历史=(前一个级别)=>。{ 返回( <scrollview style=styles.container> previousLevels.reverse().map(日期,应力值,轮胎压力值)=>。{ 返回( 卡特KBT 键= {日期} 日期={日期} 应力值应力值 轮胎压力值=轮胎压力值 /gt; ) } } </scrollview> ) } … 导出默认历史记录 < /代码>

    如此代码中所示(如下), history的属性仅在用户按save后更新。

    < Buff行情>

    APS.JS

    < /块引用> 从'react'导入react 从'react native'导入视图、滚动视图、样式表 从“expo”导入apploading,字体 从“react native simple store”导入存储 从“Lodash”导入“Debounce” 从“./components/currentlevels”导入currentlevels 从“./components/history”导入历史记录 导出默认类app扩展react.component{ 建造师(道具){ 超级(道具) 这个状态={ IsLoadingComplete:错误, 当前级别:{ 压力值:1, 轮胎压力值:1, } 上一级:【】, } this.debounce更新应力值=debounce(this.onchange应力值,50) this.debounce更新轮胎压力值=debounce( 一旦改变轮胎压力值, 五十 ) } 异步组件didmount()。{ const previousLevels=等待store.get('previousLevels') if(前一级){ this.setState(前一级) } } RelDead(){ const stressvalue,tirednessvalue=this.state.currentLevels 如果(!)this.state.isloadingcomplete&!这个.道具.滑雪板屏幕){ 返回( 掌声 … /gt; ) }否则{ 返回( <view style=flex:1> <当前级别 应力值应力值 onChangeStressValue=this.debounceUpdateStressValue_ 轮胎压力值=轮胎压力值 onChangeTiredNessValue=this.debounceUpdateTiredNessValue_ onsave=this.onsave_ /gt; <history previouslevels=this.state.previouslevels/> &Lt/VIEW & GT; ) } } … onChangeStressValue=StressValue=>。{ const轮胎压力值=this.state.currentLevels this.setState(当前级别:应力值,轮胎压力值) } onchangeTireDnessValue=TireDnessValue=>。{ const stressvalue=this.state.currentLevels this.setState(当前级别:应力值,轮胎压力值) } OnSave=()=>。{ store.push('previousLevels','{ 日期:`$new date()`, …此.state.currentlevels, })。然后(()=>。{ store.get('previousLevels').then(previousLevels=>){ 这个SETStATE({ 电流等级:应力值:1,轮胎压力值:1, 前期水平, }) }) }) } } < /代码> < Watch the issue

    这不应该发生,因为道具传给了 History 组件尚未更新。

    /组件/历史.js

    ...
    const History = ({ previousLevels }) => {
      return (
        <ScrollView style={styles.container}>
          {previousLevels.reverse().map(({ date, stressValue, tirednessValue }) => {
            return (
              <CardKBT
                key={date}
                date={date}
                stressValue={stressValue}
                tirednessValue={tirednessValue}
              />
            )
          })}
        </ScrollView>
      )
    }
    ...
    export default History
    

    如下面的代码所示,该属性 历史 仅在用户按“保存”后更新。

    App.JS

    import React from 'react'
    import { View, ScrollView, StyleSheet } from 'react-native'
    import { AppLoading, Font } from 'expo'
    import Store from 'react-native-simple-store'
    import { debounce } from 'lodash'
    
    import CurrentLevels from './components/CurrentLevels'
    import History from './components/History'
    
    export default class App extends React.Component {
      constructor(props) {
        super(props)
    
        this.state = {
          isLoadingComplete: false,
          currentLevels: {
            stressValue: 1,
            tirednessValue: 1,
          },
          previousLevels: [],
        }
    
        this.debounceUpdateStressValue = debounce(this.onChangeStressValue, 50)
        this.debounceUpdateTirednessValue = debounce(
          this.onChangeTirednessValue,
          50
        )
      }
    
      async componentDidMount() {
        const previousLevels = await Store.get('previousLevels')
        if (previousLevels) {
          this.setState({ previousLevels })
        }
      }
    
      render() {
        const { stressValue, tirednessValue } = this.state.currentLevels
    
        if (!this.state.isLoadingComplete && !this.props.skipLoadingScreen) {
          return (
            <AppLoading
              ...
            />
          )
        } else {
          return (
            <View style={{ flex: 1 }}>
              <CurrentLevels
                stressValue={stressValue}
                onChangeStressValue={this.debounceUpdateStressValue}
                tirednessValue={tirednessValue}
                onChangeTirednessValue={this.debounceUpdateTirednessValue}
                onSave={this.onSave}
              />
              <History previousLevels={this.state.previousLevels} />
            </View>
          )
        }
      }
    
    ...
    
      onChangeStressValue = stressValue => {
        const { tirednessValue } = this.state.currentLevels
        this.setState({ currentLevels: { stressValue, tirednessValue } })
      }
    
      onChangeTirednessValue = tirednessValue => {
        const { stressValue } = this.state.currentLevels
        this.setState({ currentLevels: { stressValue, tirednessValue } })
      }
    
      onSave = () => {
        Store.push('previousLevels', {
          date: `${new Date()}`,
          ...this.state.currentLevels,
        }).then(() => {
          Store.get('previousLevels').then(previousLevels => {
            this.setState({
              currentLevels: { stressValue: 1, tirednessValue: 1 },
              previousLevels,
            })
          })
        })
      }
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Kamalakannan J    8 年前

    PureComponent shouldComponentUpdate()

    Component

    推荐文章