反应setState函数不起作用

奇迹

我是React的新手,我一直在尝试在react中应用事件处理,但是在onclick事件上遇到一些问题。当“显示德国”按钮被称为“页面”时,则仅锁定加载...

这是我写的代码..

class App extends Component {
    constructor(props) {
        super(props);

        this.state = { articles: [],
                isLoaded:false ,
                country:'us'
                }
       
        this.change = this.change.bind(this);
    }


    componentDidMount() {
        const APIurl = `https://newsapi.org/v2/top-headlines?country=${this.state.country}&apiKey=${API_KEY}`;
        fetch(APIurl)
            .then(response => response.json())
            .then(json => {
                this.setState({
                    articles: json.articles,
                    isLoaded:true
                })
            })
    }
   // function to change the state
    change()
    {
        this.setState({
            articles: [],
            isLoaded:false ,
            country:"de"

        })
    }   
   
    render() {
      const { isLoaded,articles } = this.state;
      if(!isLoaded)
      {
          return (<h1>Loading....</h1>)
      }
     
        return (
            <div>
                <Navbar/>
                <button onClick={this.change}>show me of germany</button>
                <ul>
                    {articles.map(item=>(
                           <News item={item}/>
                    ))}
                </ul>
            </div>
       );
   }
}
export default App;

希望你理解问题

基里尔·斯科马洛夫斯基(Kirill Skomarovskiy)

您必须再次请求。

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      articles: [],
      isLoaded: false,
      country:'us'
    }
       
    this.change = this.change.bind(this);
  }


  componentDidMount() {
    fetchData(this.state.country);
  }

  componentDidUpdate(prevProps, prevState) {
    const { country: prevCountry } = prevState;
    const { country: nextCountry } = this.state;

    if (prevCountry !== nextCountry) {
      fetchData(nextCountry);
    }
  }

  change() {
    this.setState({ country: 'de' });
  }

  fetchData(country) {
    this.setState({ articles: [], isLoaded: false });

    fetch(
      `https://newsapi.org/v2/top-headlines?country=${country}&apiKey=${API_KEY}`
    )
      .then(res => res.json())
      .then(({ articles }) => {
        this.setState({ articles, isLoaded: true });
      })
      .catch(console.error);
  }
   
  render() {
    //...
  }
}

export default App;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

反应本地this.setState不起作用

来自分类Dev

setState在onChange函数中不起作用

来自分类Dev

在compnonentDidMount上反应本机setState不起作用

来自分类Dev

反应setState和array.push不起作用

来自分类Dev

使用事件属性反应setState不起作用

来自分类Dev

在compnonentDidMount上反应本机setState不起作用

来自分类Dev

反应“获取”响应成功,但 setState 不起作用

来自分类Dev

反应挂钩useState的setter内部函数不起作用

来自分类Dev

导出后反应函数导出不起作用

来自分类Dev

反应 在组件函数中导入函数时,我能知道为什么它不起作用会钩住setState ..吗?

来自分类Dev

反应colspan不起作用

来自分类Dev

反应动画不起作用

来自分类Dev

反应绑定不起作用

来自分类Dev

反应冒泡不起作用

来自分类Dev

反应语法不起作用

来自分类Dev

我的反应状态在函数中不起作用,但是在呈现时起作用

来自分类Dev

反应:如何正确更新状态数组?setState(state.concat(array))不起作用

来自分类Dev

setState在React的setInterval内部不起作用

来自分类Dev

为什么“ this.setState”不起作用?

来自分类Dev

抖动更新列表,setstate不起作用

来自分类Dev

TypeScript React setState 不起作用

来自分类Dev

流星:反应性:假不起作用

来自分类Dev

反应本机TextInput边框不起作用

来自分类Dev

Backgroundimage在反应中不起作用

来自分类Dev

简单的反应渲染不起作用

来自分类Dev

反应父子组件不起作用

来自分类Dev

反应本机firebase getToken()不起作用

来自分类Dev

使用useRef反应createPortal不起作用

来自分类Dev

反应导航嵌套导航不起作用