Skip to content

Using React Hooks initialize Data, But Not Work Correctly #26059

Closed
@LiuL0703

Description

@LiuL0703

here is my code

import React, {Fragment,useState, useEffect,useReducer} from 'react';
import {
  SafeAreaView,
  ScrollView,
  View,
  Text,
  TouchableHighlight,
  FlatList
} from 'react-native';


const HomePage = (props) => {
  const [selectedName,setselectedName] = useState('');
  const [nameList, setNameList] = useState([{'name':'abcd'},
      {'name':'abcd'},
      {'name':'abcd'},
      {'name':'abcd'}
  ]);

  function handleSelectName(value){
    setselectedName(value);
  }


  return (
    <Fragment>
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic">
          <View>
            { nameList.length > 0 && CustomCount(nameList,selectedName,handleSelectName) }
          </View>
        </ScrollView>
      </SafeAreaView>
    </Fragment>
  );
};


function CustomCount(data,count,handle){
  const counterStyle = {
    fontSize: 30,
    color: 'red',
  };
  return (
    <FlatList
      data={data}
      renderItem={({ item, index }) => (
        <View key={item}>
          <TouchableHighlight 
            onPress={()=>handle(index)} 
          >
            <Text style={ index === count ? counterStyle : {color: 'green'}} >{item.name}</Text>
          </TouchableHighlight>
        </View>
      )}
      numColumns={3}
    />
  )
}

export default HomePage;

What I expected

when i clicked the name, which name's style changes

What actually did

nothing happened

env

"react": "16.8.6",
"react-native": "0.60.4",

Metadata

Metadata

Assignees

No one assigned

    Labels

    Ran CommandsOne of our bots successfully processed a command.Resolution: LockedThis issue was locked by the bot.Type: QuestionIssues that are actually questions and not bug reports.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions