Javascript 如何从API获取具有静态对象位置的字符串

Javascript 如何从API获取具有静态对象位置的字符串,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我只需要从这个数组的第一个位置获取字符串 第一个位置上只有标题“星球大战” 但我的代码返回空值 但我的警报是返回空的xName。无法从title设置状态要获得《星球大战》的标题,您需要获得responseJson.movies[0]。title而不是responseJson.title[0]我会这样做 class movieFetch extends Component{ constructor(props){ super(props);

我只需要从这个数组的第一个位置获取字符串

第一个位置上只有标题“星球大战”

但我的代码返回空值






但我的警报是返回空的xName。无法从title设置状态要获得《星球大战》的标题,您需要获得
responseJson.movies[0]。title
而不是
responseJson.title[0]
我会这样做

 class movieFetch extends Component{
        constructor(props){
            super(props);
            this.state = {movies: ''};
        }

    componentDidMount() {
         fetch('https://facebook.github.io/react-native/movies.json')
            .then((response) => response.json())
            .then((responseJson) => {this.setState({movies: responseJson.title});})
            .catch((error) => {
                console.error(error);
            })
    }

    render(){
        return (
          alert({this.state.movies[0].title}),
          <h3>{this.state.movies[0].title}</h3>
        );
    }
类movieFetch扩展组件{
建造师(道具){
超级(道具);
this.state={movies:'};
}
componentDidMount(){
取('https://facebook.github.io/react-native/movies.json')
.then((response)=>response.json())
.then((responseJson)=>{this.setState({movies:responseJson.title});})
.catch((错误)=>{
控制台错误(error);
})
}
render(){
返回(
警报({this.state.movies[0].title}),
{this.state.movies[0].title}
);
}

尚未测试它,但可能正在尝试此
responseJson.movies[0]。title
它再次返回null:(
setState())
不会立即变异
此.state
因此可以在`componentDidMount`外部尝试,就像在
render
方法内部一样。什么?setState可以工作…它可以从setState on componentDidMount和{this.state.xName}处理我的代码但是在警报中无法工作它的xName返回null我说的
setState
不工作了吗?就像我说的
setState()
不会立即变异(更新)state,在
setState
方法之后调用this.state可能会返回现有值。您可以在此处了解更多信息,您的api是否返回任何数据?请将获取块更改为此获取(')。然后(response=>{return response.json();})。然后(json=>{this.setState({movies:json.movies[0].title}});});}这是渲染工作。但警报无法工作。:)。您没有以正确的方式渲染它。阅读关于React的教程,提问不会帮助你理解这是一个好的开始point@boy_v回答了你的两个问题,还没有接受它们作为答案吗?如果它解决了你的问题,请将它标记为答案。
constructor(props){
  super(props)
  this.state = {
    xName: '',
  };

}
componentDidMount() {
     fetch('https://facebook.github.io/react-native/movies.json')
        .then((response) => response.json())
        .then((responseJson) => 
        {this.setState({xName: responseJson.title[0]});})
        .catch((error) => {
            console.error(error);
        })

  alert(this.state.xName);
}
 class movieFetch extends Component{
        constructor(props){
            super(props);
            this.state = {movies: ''};
        }

    componentDidMount() {
         fetch('https://facebook.github.io/react-native/movies.json')
            .then((response) => response.json())
            .then((responseJson) => {this.setState({movies: responseJson.title});})
            .catch((error) => {
                console.error(error);
            })
    }

    render(){
        return (
          alert({this.state.movies[0].title}),
          <h3>{this.state.movies[0].title}</h3>
        );
    }