简体   繁体   English

完全在 React 中渲染组件返回的组件

[英]Rendering Component Returned Component Completely In React

I'm creating an app and I need to render a form element multiple times with some of the variable names changed around.我正在创建一个应用程序,我需要多次渲染一个表单元素,并更改一些变量名称。 Unfortunately, I'm having some issues where not all of the code is being returned from the component.不幸的是,我遇到了一些问题,即并非所有代码都从组件返回。 I've taken out some of the variable declarations to make this easier to read.我去掉了一些变量声明,以便于阅读。

Primary Component主要成分

export default function StrengthLog() {
   
    return (
        <Container id="strengthContainer">
            <SelectMovements />
            <Form>
            {
                movement.map(movement => {
                    const checked = movement[1].triggered
                    if (checked){
                        return <StrengthForm props={movement}/>
                    }
                })
            }
            <Button content='submit'>Submit</Button>
            </Form>
        </Container>
    )
}

Component being returned组件被退回

export default function StrengthForm({props}) {
    return (
        <div>
            <h1>Hello!</h1>
                {               
                    props[1].triggered
                    ? (
                        setsArray.map(item => {
                            return <Group widths='equal'>
                            <Select fluid label='Reps' placeholder='Select Reps' options={repOptions} name={`${liftVarName}-rep-1`}/>
                            <Input fluid placeholder='Enter Weight' label='Weight' name={`${liftVarName}-weight-1`}/>
                            <Button className="addSet">+</Button>
                            <Button className="deleteSet">x</Button>
                            </Group>
                        })
                    )
                    : console.log('Thing')
                 }
        </div>
    )
}

What's happening is that it's returning the <h1> of StrengthForm but it's not returning the other information contained within the curly braces.发生的事情是它返回了StrengthForm<h1>但它没有返回大括号中包含的其他信息。

The issue was that I was declaring setsArray as const setsArray = new Array(props[1].sets) which gives me StrengthForm.js:9 (4) [empty × 4] in the console.问题是我将setsArray声明为const setsArray = new Array(props[1].sets)这在控制台中给了我StrengthForm.js:9 (4) [empty × 4] I suspected because the array actually had no content that I needed to add something there so I commented my initial declaration and did this instead.我怀疑是因为数组实际上没有我需要在那里添加一些内容的内容,所以我评论了我的初始声明并改为这样做。

    const setsArray = (() => {
        const emptyArray = []
        for(let i = 0; i <= props[1].sets; i++) {
            emptyArray.push(' ');
        }
        return emptyArray;
    })();
    return (
        <div>
            <h1>Hello!</h1>
                {               
                    props[1].triggered
                    ? (
                        setsArray.map(() => {
                            return <>
                            <Group widths='equal'>
                            <Select fluid label='Reps' placeholder='Select Reps' options={repOptions} name={`${liftVarName}-rep-1`}/>
                            <Input fluid placeholder='Enter Weight' label='Weight' name={`${liftVarName}-weight-1`}/>
                            <Button className="addSet">+</Button>
                            <Button className="deleteSet">x</Button>
                            </Group>
                            </>
                        })
                        
                    )
                    : console.log('Thing')
                 }
        </div>
    )

Everything popped up after I changed it to this!我把它改成这个后,一切都弹出了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM