V
V
Vanya Huk2018-02-27 12:56:59
React
Vanya Huk, 2018-02-27 12:56:59

How to iterate over props in react.js?

here I am passing the parameters

window._sharedData = {
                    "account": {"personal_info":"\u041b\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435","settings":"\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438","sign_out":"\u0412\u044b\u0445\u043e\u0434"}
            }

in the console it looks like this:
5a952acf7c91f594189352.png
below I connect the script with react
export default class Root extends Component {
    
    render() {
        return (
            <MuiThemeProvider>
                <AppBar
                    title=""
                    showMenuIconButton={false}
                    iconElementRight={
                        <IconMenu
                        iconButtonElement={<IconButton><MoreVertIcon /></IconButton>}
                        anchorOrigin={{horizontal: 'right', vertical: 'top'}}
                        targetOrigin={{horizontal: 'right', vertical: 'top'}}
                    >

                            { this.props._sharedData.account.map((item, i) => (

                                <MenuItem primaryText={item}  />

                                ))}


                    </IconMenu>
                    }
                >
                </AppBar>
            </MuiThemeProvider>
        );
    }
}


if (document.getElementById('root')) {
    ReactDOM.render(<Root _sharedData={window._sharedData} />, document.getElementById('root'));
}

and I get an error
5a952b344504b142709922.pngfor() doesn't work either

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Aleksandrovich, 2018-02-27
@vanyahuk

Try this

{ Object.keys(this.props._sharedData.account).map(
(item, i) => (
                                <MenuItem primaryText={this.props._sharedData.account[item]}  />
                                )
)}

M
Maxim, 2018-02-27
@maxfarseer

In addition to Roman's answer - learn to read errors. What you have written: this.props._sharedData.account.map is not a function. Why? Because this.props._sharedData.account.map = undefined and undefined() = error with such text.
And all why? because the map method only works for arrays! And you have an object. To make it easier for you to learn React and have good progress - pull up the basics (you can learn.javascript.ru)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question