Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何迭代json响应而不在reactjs中使用jsx?_Reactjs - Fatal编程技术网

如何迭代json响应而不在reactjs中使用jsx?

如何迭代json响应而不在reactjs中使用jsx?,reactjs,Reactjs,我是react的新手,我对如何在不使用jsx的情况下迭代json响应有些怀疑,因为我只能使用react在html文件中添加脚本标记。巴别塔,这不是一个选择 我发现这是为了将jsx转换为createElement,但它对eather不起作用 这是我的json响应: { "user": { "userId": 1, "fullName": "xxxxx", "ldapId": "xxxx", "contraseña": "xxx

我是react的新手,我对如何在不使用jsx的情况下迭代json响应有些怀疑,因为我只能使用react在html文件中添加脚本标记。巴别塔,这不是一个选择

我发现这是为了将jsx转换为createElement,但它对eather不起作用

这是我的json响应:

{
    "user": {
        "userId": 1,
        "fullName": "xxxxx",
        "ldapId": "xxxx",
        "contraseña": "xxxxxx!",
        "email": "c@mail",
        "deleteFlag": "N ",
        "access": "Y ",
        "roles": [
            {
                "roleId": 2,
                "roleName": "Admin",
                "focusAccount": "N ",
                "channelAccount": "N ",
                "menuHome": null
            },
            {
                "roleId": 1,
                "roleName": "Dashboard",
                "focusAccount": "N ",
                "channelAccount": "N ",
                "menuHome": null
            }
        ]
    }
}
代码:

class User extends React.Component {
  state = {
    user: []
  }

  componentDidMount() {
    axios.get('http://localhost:8080/dashboard?user=' + localStorage.getItem("user"))
      .then(res => {
        const user = res.data;
        this.setState({ user });
      })
  }

  React.createElement("ul", null, (void 0).state.user.map(function (user) {
  return React.createElement("li", null, user.fullName);
  }))

}

ReactDOM.render(User, document.getElementById('root'));
在这种情况下,我假设即使我正在对用户进行映射,并且我知道该用户只是一个,它也应该在ul标记内创建一个li标记


提前感谢。

如果您只是询问关于json上的迭代,它非常简单,我发现您有一个对象,并且您正在使用map进行迭代,因此您只需稍作更改,它就可以工作了

我只回答迭代问题,并使用
Object.keys
您也可以在json内部进行迭代


在这种情况下,您试图映射到一个对象,而
user
是一个对象。如果要作为阵列运行,可以使用。Array.from或
[state.user].map
回答得好,谢谢!还有一件事,我正试图用ReactDOM.render(User,document.getElementById('root'))来呈现它;其中root是一个空div标记的id,但这不起作用,是吗?我编辑了js文件。你可以试试这个@CarolinaPonce ReactDOM.render(React.createElement(User,{},null),document.getElementById('root');非常感谢你!我花了几个小时来解决这个问题!

class User extends React.Component {
  state = {
    user: {}
  }

  componentDidMount() {
        axios.get('http://localhost:8080/dashboard?user=' + localStorage.getItem("user"))
        .then(res => {
            const user = res.data;
            this.setState({ user });
        })
    }

    render () {
        const {user} = this.state
        return React.createElement("ul", null, Object.keys(user).map( data => ( 
            React.createElement("li", null, user[data].fullName)
        )))
    }
}

export default User