我對ReactJs非常陌生,目前正在努力創建一個onClick事件,它將採用自身的href值並將其傳遞給一個名爲'action'的函數。未捕獲(承諾)ReferenceError:<function>未定義
動作函數應該阻止默認的瀏覽器行爲,對傳入的URL執行GET請求,並顯示包含響應和狀態的警報對話框。
但是我堅持試圖與下面的錯誤叫我函數從onClick事件:
Uncaught (in promise) ReferenceError: action is not defined
var Content = React.createClass({
getInitialState: function() {
return {
teams: []
}
},
componentDidMount: function() {
const makeRequest =() => axios.get("http://localhost:5000/api/teams").then(({ data }) => this.setState({ teams: data }));
this.serverRequest = makeRequest();
this.poll = setInterval(() => {
this.serverRequest = makeRequest();
}, 10000)
},
componentWillUnmount: function() {
this.serverRequest.abort();
clearInterval(this.poll)
},
action: function(e) {
e.preventDefault();
$.get(e.href, function(res, status) {
alert("Response: " + res + "\nStatus: " + status);
});
},
render: function() {
const { teams } = this.state;
return (
<div className="list-group">
{ teams.map(function(team) {
return ([
<a href={ "http://localhost:5000/api/teams?name=" + team.name} onClick={ action }>Click Me</a>
]);
})}
</div>
)
}
});
ReactDOM.render(<Content />, document.getElementById('content'));
有什麼不清楚的錯誤? 'render'中不存在變量'action'。 –