javascript - 如何通过调用我的方法 en React 使我的 url 工作?
问题描述
我的代码中存在问题,在我的方法componentDidMount
中,当我在我的方法中调用我的变量 URL 时,this.handleClick(url)
它让我犹豫不决,但是当我console.log
从url
它创建一个有效的时,它会在我单击任何按钮时生成一个链接!
我如何让我的网址在其中工作this.handleclick()
?
const { Component } = React; // import React, { Component } from 'react';
const { Button } = ReactBootstrap; // import Button from './components/Button';
class App extends Component {
constructor() {
super();
this.state = {
name: '',
capital: "",
flag: "",
population: 0,
}
}
handleClick = (country) => {
fetch(country)
.then(res => res.json())
.then(json =>
this.setState({
name: json[0].name,
capital: json[0].capital,
flag: json[0].flag,
population: json[0].population,
region: json[0].region,
})
);
console.log(this.state)
console.log(country)
}
componentDidMount(click) {
const url = `https://restcountries.eu/rest/v2/name/${click}`;
this.handleClick()// here i have to call
console.log(url)
}
render() {
return (
<div className="App">
<p> name= {this.state.name}</p>
<p> capital= {this.state.capital}</p>
<p> flag= {this.state.flag}</p>
<p> population= {this.state.population}</p>
<p> region= {this.state.region}</p>
<Button onClick={this.componentDidMount.bind(this, 'france')}>France</Button>
<Button onClick={this.componentDidMount.bind(this, 'brazil')}>Brazil</Button>
<Button onClick={this.componentDidMount.bind(this, 'croatia')}>Croatia</Button>
</div>
);
}
}
ReactDOM.render(<App />, document.querySelector("#app")); // export default App;
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/react-bootstrap@1/dist/react-bootstrap.min.js"></script>
<div id="app"></div>
解决方案
直接打电话就handleClick
行了。
componentDidMount 是一个反应生命周期,不要将它绑定到点击事件!
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.5.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.5.0/umd/react-dom.production.min.js"></script>
import React, { Component } from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import Button from './components/Button';
class App extends Component {
constructor() {
super();
this.state = {
name: '',
capital: "",
flag: "",
population: 0,
}
}
handleClick = (click) => {
const url = `https://restcountries.eu/rest/v2/name/${click}`;
fetch(url)
.then(res => res.json())
.then(json =>
this.setState({
name: json[0].name,
capital: json[0].capital,
flag: json[0].flag,
population: json[0].population,
region: json[0].region,
})
);
console.log(this.state)
console.log(country)
}
render() {
return (
<div className="App">
<p> name= {this.state.name}</p>
<p> capital= {this.state.capital}</p>
<p> flag= {this.state.flag}</p>
<p> population= {this.state.population}</p>
<p> region= {this.state.region}</p>
<Button onClick={this. handleClick.bind(this, 'france')}>France</Button>
<Button onClick={this. handleClick.bind(this, 'brazil')}>Brazil</Button>
<Button onClick={this. handleClick.bind(this, 'croatia')}>Croatia</Button>
</div>
);
}
}
export default App;
推荐阅读
- reactjs - 如何在孩子重新渲染时让父视图保持大小?
- android - 关闭并重新打开应用程序时,Android Toolbar 会删除所有菜单项
- keycloak - 使用 https 时 IDP 元数据在位置 URL 中丢失端口
- java - 本地主机的 InetSocketAddress
- php - PHP 获取多个数据
- php - get_headers 不同的结果 xampp localhost
- angular - 来自 FormBuilder 的角度转换。样式到 FormGroup。如何创建Formarray?
- ruby-on-rails - 如何通过 Ruby on Rails 中的 API GET 请求访问(并保存到我的数据库)JSON 数组中的嵌套对象/属性?
- jenkins - 将构建信息发布到来自 jenkins 的通用人工仓库
- scala - Scala Play 2.8 Framework:分割路由文件,前缀包含动态部分