【问题标题】:How to refer to a single element of a mapped array to render a View如何引用映射数组的单个元素来呈现视图
【发布时间】:2019-08-30 20:28:42
【问题描述】:

我有一个对象数组。我把它映射成一个带有标签和图标按钮的列表。 当我点击按钮时,我需要在该行的元素下方显示另一个视图。

对象数组是一个let变量。我想把它作为一个状态(因为在执行 setState 函数时会重新渲染),但我需要它作为一个 let 变量,因为它们是来自导航路线的数据

我的想法是在数组的每个对象中添加一个标志,以指示何时必须渲染视图。这在开始时是错误的,当我按下按钮时是真的,但什么也没发生。如果在我点击按钮时不使用标志,则视图会在每一行中呈现

state={
   visible:false
}

getNewView = (item) => {
        if (this.state.visible && item.showViewBelow)
            return (
                <View>
                    <HiddenViewComponent/>
                </View>
            )
};

btnHandler = (item) => {
   item.showViewBelow = true;
   this.setState({visible:true})
}


render(){
   const {navigation} = this.props;
   const arrayNav = navigation.getParam('data', '');

   let array = [
            {
                id:'0',
                label: arrayNav.label1,
                showViewBelow: false
            },
            {
                id:'1',
                label: arrayNav.label2,
                showViewBelow: false
            },
            {
                id:'2',
                label: arrayNav.label3,
                showViewBelow: false
            }
   ]
return(
  <View>
     array.map((item, key)=> { return

         <View key = {key} style={{marginHorizontal: 25}}>

              <Text>{item.label}</Text>

              <TouchableOpacity onPress={this.btnHandler(item)}>
                 <Image source=(blablabla)/>
              </TouchableOpacity>

              {this.getNewView(item)}

         </View>)

     )}
  </View>
)
 }

所以,当我单击列表的一行时,我希望在此行下方显示一个新视图。

我尝试将其中一个对象中的标志切换为 true,而在其他对象中将其设置为 false。它在那条线上工作得很好!当我单击该行中的按钮时,新视图会正确呈现在该行下方。

我也尝试使用平面列表,并在新视图返回后将一些带有状态变量的额外数据用 setState 进行更新。 (只是为了尝试重新渲染页面)。

不过,我还是尝试将 this.forceUpdate() 放在 getNewView 方法中。什么都没发生。什么都没有渲染

所以我认为问题在于我没有重新渲染整个屏幕,因为将对象作为 let 变量,这不会发生。

有什么想法吗?

【问题讨论】:

    标签: javascript typescript react-native


    【解决方案1】:

    您可以在常量实用程序中创建方法来定义 defaultArray 之类的

    getDefaultArray(arrayNav){
    
        return [
            {
                id:'0',
                label: arrayNav.label1,
                showViewBelow: false
            },
            {
                id:'1',
                label: arrayNav.label2,
                showViewBelow: false
            },
            {
                id:'2',
                label: arrayNav.label3,
                showViewBelow: false
            }
       ];
    
    }
    

    在你的组件代码中

    import Constants from "./Constants"
    
    ComponentDidMount(){
    
        const {navigation} = this.props;
        const arrayNav = navigation.getParam('data', '');
    
        this.state={
            visible:false,
            array = Constants.getDefaultArray(arrayNav);
        }
    }
    
    getNewView = (item) => {
            if (this.state.visible && item.showViewBelow)
                return (
                    <View>
                        <HiddenViewComponent/>
                    </View>
                )
    };
    
    btnHandler = (item, index) => {
    
       const {navigation} = this.props;
       const arrayNav = navigation.getParam('data', '');
    
       let newData = Constants.getDefaultArray(arrayNav);
       newData[index].showViewBelow = true;
       this.setState({visible:true , array:newData})
    }
    
    render(){
    
        return(
          <View>
             this.state.array.map((item, key)=> { return
    
                 <View key = {key} style={{marginHorizontal: 25}}>
    
                      <Text>{item.label}</Text>
    
                      <TouchableOpacity onPress={this.btnHandler(item,key)}>
                         <Image source=(blablabla)/>
                      </TouchableOpacity>
    
                      {this.getNewView(item)}
    
                 </View>)
    
             )}
          </View>
        )
     }
    

    【讨论】:

    • 我应该在没有 setState 的情况下更改 showViewBelow 吗?我的意思是在 btnHandler 函数中
    • 没有。您必须使用 setState() 更改 showViewBelow。您必须找到按下的索引,然后在状态中按索引搜索项目并更改状态,以便它反映在渲染视图上。
    • 好的,你能告诉我怎么做吗?我的意思是如何执行 setState 功能?找到索引后如何访问 array[i].showViewBelow?
    • 好的,所以问题是我不能在类中声明 car/let 变量。它必须高于导出默认类。另外,像这样声明状态(使用构造函数(props ...),当我使用状态时出现错误(我认为来自打字稿)。例如,如果我执行 this.state.visible,我会得到 Property 'visible' ReadOnly 类型中不存在
    • ReadOnly 类型中不存在“可见”属性 如果我使用 this.state={a:1, b:2},则始终存在
    猜你喜欢
    • 2020-10-04
    • 1970-01-01
    • 2011-08-25
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多