如何将数组映射到React Native[TypeScript]的复选框中?

如何将数组映射到React Native[TypeScript]的复选框中?,typescript,react-native,checkbox,store,Typescript,React Native,Checkbox,Store,我是个新来的本地人。我目前正在为我的React原生项目使用TypeScript。现在,我很难将数组映射到复选框中。下面是我的json文件: { "stud_name": "Adam", "sex": "male", "attendance": false, "eligibility": false } 我想在复选框中映射出勤和资格。下面是复选框的React本机代码 export const StudentRegisterExam: React.FC<Props&

我是个新来的本地人。我目前正在为我的React原生项目使用TypeScript。现在,我很难将数组映射到复选框中。下面是我的json文件:

{ 
   "stud_name": "Adam",
   "sex": "male",
   "attendance": false,
   "eligibility": false
}
我想在复选框中映射
出勤
资格
。下面是复选框的React本机代码

export const StudentRegisterExam: React.FC<Props> = ({}) => {
  const StudentData = useContext(StudentStoreContext);

  const firstRender = useRef(true);
  useEffect(() => {
    if (firstRender.current) {
      firstRender.current = false;
      return;
    }
    if (response !== undefined) {
      if (response.status === 201 || response.status === 200) {
        StudentData.details = {
           name: response.data.name,
           sex: response.data.sex,
           eligibility: response.data.eligibility,
           attendance: response.data.attendance
       };
     }
   }
 }, [response]);

return (
      <View>
        <Text>{StudentData.response.name}</Text>
        <Text>{StudentData.response.sex}</Text>
        <CheckBox /> <- how should I write {StudentData.response.eligibility} into checkbox
        <CheckBox />
      </View>
 );
export const StudentRegisterExam:React.FC=({})=>{
const StudentData=useContext(StudentStoreContext);
const firstRender=useRef(true);
useffect(()=>{
if(firstRender.current){
firstRender.current=false;
返回;
}
如果(响应!==未定义){
如果(response.status==201 | | response.status==200){
StudentData.details={
名称:response.data.name,
性别:response.data.sex,
资格:响应。数据。资格,
出席人数:response.data.attention
};
}
}
}[答复];
返回(
{StudentData.response.name}
{StudentData.response.sex}

我不完全明白你的意思。 映射一个数组意味着对它的每个值进行迭代。但你的意思似乎是如何将数据放入-组件中

问题是,您的复选框组件是什么样子的。您应该发布它的源代码……还是使用像
react elements
这样的UI库

如果发生以下情况,您可以这样做:

const{response}=StudentData;
返回(
{Object.keys(response.map)(项=>
{响应[项目].stud_name}
{响应[项目].sex}
) }
);
希望这能给你一个想法

想想你搜索的是如何在JSX中执行循环,不是吗? 或者是如何将参数放入组件中

因此,您可以在{}内执行它,但您只能在那里使用表达式……没有经典的if、for或while循环。
因此,使用yourArr.forEach、yourArr.map或三元运算符之类的方法调用来替换if条件

我不完全明白你的意思。 映射一个数组意味着对它的每个值进行迭代。但你的意思似乎是如何将数据放入-组件中

问题是,您的复选框组件是什么样子的。您应该发布它的源代码……还是使用像
react elements
这样的UI库

如果发生以下情况,您可以这样做:

const{response}=StudentData;
返回(
{Object.keys(response.map)(项=>
{响应[项目].stud_name}
{响应[项目].sex}
) }
);
希望这能给你一个想法

想想你搜索的是如何在JSX中执行循环,不是吗? 或者是如何将参数放入组件中

因此,您可以在{}内执行它,但您只能在那里使用表达式……没有经典的if、for或while循环。
因此,使用yourArr.forEach、yourArr.map或三元运算符等方法调用来替换if条件

我已经尝试过你的方法。但是,我遇到了一个错误
TypeError:无法读取未定义的
{response.map(item=>
不起作用。try
{response&&response.map(item=>…)的属性“map”
@HaykShakhbazyan哇!在我尝试了{response&&response.map(item=>…`之后,它工作了,但不幸的是我的复选框没有出现在屏幕上。
const{response}出现了一个错误=StudentData;
这使我的复选框不会出现在屏幕上。你知道我如何使我的复选框出现在屏幕上吗?谢谢:)对不起,我的代码中有一个问题。我在上面解决了它。当然,你必须用
对象.keys()迭代你的
对象
。这样应该可以工作。这里我还创建了一个小示例来演示它是如何工作的:我已经尝试了您的方法。但是,我遇到了一个错误
类型错误:无法读取未定义的
{response.map(item=>
不工作。请尝试
{response&&response.map(item=>…)}的属性“map”
@HaykShakhbazyan哇!在我尝试了{response&&response.map(item=>…`之后,它工作了,但不幸的是我的复选框没有出现在屏幕上。
const{response}出现了一个错误=StudentData;
这使我的复选框不会出现在屏幕上。你知道我如何使我的复选框出现在屏幕上吗?谢谢:)对不起,我的代码中有一个问题。我在上面解决了它。当然,你必须用
对象.keys()迭代你的
对象
。这样应该可以工作。这里我还创建了一个小示例来演示它是如何工作的: