简体   繁体   English

如何访问数组中的每个元素并为 ReactJS 中的数组中的所有元素创建一个页面?

[英]How to access each element in an array and make a page for all elements in the array in ReactJS?

I'm new to programming and especially to React.JS.我是编程新手,尤其是 React.JS。 I managed to make checkboxes with an array of elements such as:我设法用一系列元素制作复选框,例如:

optionsInstitutes = [' Administrasjon og ledelse', 'Datateknologi', 'Helse- og sosialfag', 'Historie, filosofi og religion', ' Idrettsfag', ' Ingeniør- og teknologiske fag', ' Kunstfag', ' Litteratur og språk', 'Lærerutdanning og pedagogikk', 'Media- og kommunikasjonsfag', 'Musikk', 'Realfag', 'Samfunnsfag', ' Økonomi og juss'] optionsInstitutes = ['Administrasjon og ledelse', 'Datateknologi', 'Helse- og sosialfag', 'Historie, filosofi og宗教', 'Idrettsfag', 'Ingeniør- og teknologiske fag', 'Kunstfag', 'Litteratur og språk', 'Lærerutdanning og pedagogikk', 'Media-og kommunikasjonsfag', 'Musikk', 'Realfag', 'Samfunnsfag', 'Økonomi og juss']

the image of the checkbox I made looks as follows:我制作的复选框的图像如下所示:

Heres, the code for the checkbox:继承人,复选框的代码:

export class Institute extends Component {


constructor(props) {
    super(props);
    this.state = {
        optionsInstitute: [],
        isLoaded: false,
    }
    
}



// Our sample dropdown options 
optionsInstitutes = [' Administrasjon og ledelse', 'Datateknologi', 'Helse- og sosialfag',
    'Historie, filosofi og religion', ' Idrettsfag', ' Ingeniør- og teknologiske fag', ' Kunstfag', ' Litteratur og språk', 'Lærerutdanning og pedagogikk', 'Media- og kommunikasjonsfag', 'Musikk', 'Realfag', 'Samfunnsfag', ' Økonomi og juss']




render() {

    return (

        <div style={{marginLeft: '7%', marginTop: '60px'}}>
            <h1>Institute</h1>
            <Autocomplete
                multiple
                id="checkboxes-tags-demo"
                options={this.optionsInstitutes}
                renderOption={(option, {selected}) => (
                    <React.Fragment>
                        <Checkbox
                            icon={icon}
                            checkedIcon={checkedIcon}
                            style={{marginRight: 8}}
                            checked={selected}
                        />
                        {option}
                    </React.Fragment>
                )}
                style={{width: 500}}
                renderInput={(params) => (
                    <TextField {...params} 
                               variant="outlined"
                               label="Checkboxes"
                               placeholder="Favorites"/>
                )}
            />

     </div>

Now the problem is, I want to make a button, onClick, that can take me to the page of the chosen array element(institute).现在的问题是,我想制作一个按钮,onClick,可以将我带到所选数组元素(研究所)的页面。 I need each of the elements in the array to have their own page because I'm going to explain and list what each institute can offer.我需要数组中的每个元素都有自己的页面,因为我将解释并列出每个机构可以提供的内容。 Thank You!谢谢你!

You can achieve this easily with react-router https://reactrouter.com/web/guides/quick-start您可以使用 react-router https://reactrouter.com/web/guides/quick-start轻松实现这一目标

And there are plenty of tutorials to work with it: https://www.freecodecamp.org/news/react-router-in-5-minutes/并且有很多教程可以使用它: https://www.freecodecamp.org/news/react-router-in-5-minutes/

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

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