当我将它传递给函数并尝试在其中访问时,React prop返回undefined

时间:2018-10-21 19:57:58

标签: reactjs react-native

//Function
const getThingName = (allCategories, id) => {
  const categoryName = _.find(allCategories, {items: [{id}]}).name;
  return categoryName;
}

//Component
const ThingsListView = ({ allCategories, .... }) => {

  console.log(JSON.stringify(allCategories))

  return (
       <ScrollView>
          <View>
               ...
            {(thingsByUser &&
              thingsByUser.length && (
                <List>
                  {thingsByUser.map(({ id }, idx) => (
                     ...
                    <CardText>{getThingName(allCategories, id)}</CardText>

console.log(JSON.stringify(allCategories))返回正确的结果,如果我不进行以下函数调用:getThingName(allCategories, id)

但是当我打电话时,console.log(JSON.stringify(allCategories))返回的是不确定的。

奇怪但真实!!我的目标是访问getThingName()中的allCategories数组值。

0 个答案:

没有答案
相关问题