即使按照SO中其他问题中的建议,我仍然无法使onClick函数try:
value = int(userInput)
except ValueError:
print("That's not an int!")
正常工作:
def calc_area(radius):
return (radius **2) * (math.pi)
def calc_circ (radius):
return (math.pi * radius) * 2
try:
radius = float(input('Please enter the cirlcle\'s radius: '))
print ('The area of the the circle is', calc_area(radius), 'and the circumference is', calc_circ(radius))
except ValueError:
print("That's not a number!")
我已尝试使用this.changeContent
,箭头功能(如此处),普通功能参考......几乎所有我在SO和其他地方看过的变体。我最初只有import React from 'react';
import Maxim from './Maxim';
import Challenge from './Challenge';
import Prompt from './Prompt';
class ContentContainer extends React.Component {
constructor() {
super();
this.changeContent = this.changeContent.bind(this);
this.state = {
content: "maxim"
}
}
changeContent(event) {
console.log(this);
if (this.state.content == "maxim") {
this.setState({ content: "challenge" })
} else {
this.setState({ content: "maxim" })
}
}
render() {
let renderedContent = null;
if (this.state.content == "challenge") {
renderedContent = <Challenge content="The trick to presence is to listen to the space between the sounds." />;
} else {
renderedContent = <Maxim quote="Silence can be heard in every sound. All you need is to listen." />;
}
return (
<div className="content-container">
{renderedContent}
<Prompt onClick={this.changeContent}/>
</div>
);
}
}
export default ContentContainer;
,并且在页面加载时调用了该函数。我至少已经解决了这个问题,但功能仍然没有被调用 - bind
都没有被更改,控制台也没有记录this.changeContent()
。
谢谢。
答案 0 :(得分:2)
检查此工作示例,以类似的方式编写它:
class ContentContainer extends React.Component {
constructor() {
super();
this.changeContent = this.changeContent.bind(this);
this.state = {
content: "maxim"
}
}
changeContent(event) {
let content = this.state.content;
this.setState({ content: content == "challenge" ? "maxim" : "challenge"})
}
render() {
let renderedContent = null;
if (this.state.content == "challenge") {
renderedContent = <Challenge content="The trick to presence is to listen to the space between the sounds." />;
} else {
renderedContent = <Maxim quote="Silence can be heard in every sound. All you need is to listen." />;
}
return (
<div className="content-container">
{renderedContent}
<Prompt onClick={this.changeContent}/>
</div>
);
}
}
class Challenge extends React.Component{
render(){
return(
<div>Inside Challenge<br/>{this.props.content}</div>
)
}
}
class Maxim extends React.Component{
render(){
return(
<div>Inside Maxim<br/>{this.props.quote}</div>
)
}
}
class Prompt extends React.Component{
render(){
return(
<div style={{paddingTop: '100px'}}>
Inside Prompt<br/>
<span onClick={()=>this.props.onClick()}>*Click Me to change the Component</span>
</div>
)
}
}
ReactDOM.render(<ContentContainer/>, document.getElementById('app'))
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app'/>
&#13;
答案 1 :(得分:-1)
<Prompt onClick={this.changeContent.bind(this)}/>
如果您还没有