如何在React JS中添加数据

How to append data in React JS

本文关键字:添加 数据 JS React      更新时间:2023-09-26

我需要在我的React应用程序中添加这个数据响应示例。

<<p> 数据响应/strong>
[
  {
    "trackInfo": {
      "id": 1,
      "title": "Guns & Dogs",
      "artist": "Portugal, The Man",
      "album": "The Satanic Satanist"
    },
    "trackUrl": "https://s3-us-west-2.amazonaws.com/teddarcuri.monarch/Portugal.+The+Man+-+Guns+%26+Dogs+-+The+Satanic+Satanist.mp3",
    "albumArt": "http://ecx.images-amazon.com/images/I/61X7CiBpZ6L.jpg"    
  }
]

反应JS

class App extends React.Component {
  constructor(props){
    super(props)
    this.state = { //initial empty details
      details : {}
    }
  }
  componentDidMount(){
    //place the ajax call where ever you need
    $.ajax() //call ajax
    .done((data) => {
      this.setState({ //this setState will re render the UI with the new state
        details: { //change the key value pairs as needed
          id: data.id,
          trackInfo: {
            title: data.title,
            artist: data.artist,
            album: data.album,
          },
          trackUrl: data.trackUrl,
          albumArt: data.albumArt,
        }
      })
    })
  }
    render() {
      if(!this.state.details.id) return false //renders nothing when no details available
        return (
            <div id="app">
                <MusicPlayer
                    id={this.state.details.id}
                    visualizerType="RIPPLES"
                    theme={darkTheme}
                    trackInfo={this.state.details.trackInfo}
                    trackUrl={this.state.details.trackUrl}
                    albumArt={this.state.details.albumArt}
                    utilities={true}>
                </MusicPlayer>
            </div>
        )
    }
}
ReactDOM.render(
    <App />,
    document.getElementById("app")
);
  • 完整的代码示例在这里
  • 使用预加载数据的工作示例

所以我的问题是,我如何使用Ajax在React中添加新数据?

我认为你想显示一个MusicPlayer列表,所以我改变了你的代码:[你需要在react中阅读更多关于state的信息]

class App extends React.Component {
  constructor(props){
    super(props)
    this.state = { //initial empty details
      details : []  // use array
    }
  }
  componentDidMount(){
    //place the ajax call where ever you need
    $.ajax() //call ajax
    .done((data) => {
      let array = this.state.details;
      array = [...array, {
          id: data.id, 
          trackInfo: {
            title: data.title,
            artist: data.artist,
            album: data.album,
          },
          trackUrl: data.trackUrl,
          albumArt: data.albumArt,
      }];
      this.setState({
        details: array
      })
    })
  }
    render() {
      if(!this.state.details.id) return false //renders nothing when no details available
        return (
            <div id="app">
                {
                    this.state.details.map((detail) => {
                        return <MusicPlayer
                    id={detail.id}
                    visualizerType="RIPPLES"
                    theme={darkTheme}
                    trackInfo={detail.trackInfo}
                    trackUrl={detail.trackUrl}
                    albumArt={detail.albumArt}
                    utilities={true}>
                </MusicPlayer>
                    });
                }
            </div>
        )
    }
}
ReactDOM.render(
    <App />,
    document.getElementById("app")
);