【发布时间】:2019-07-08 00:03:32
【问题描述】:
我试图映射一个数组,该数组由 if-else 语句的控制呈现,但我想在该 if-else 语句中调用一个函数,但它不起作用。为什么?
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { NavLink } from "react-router-dom";
import { getQuiz, sendResponse } from "../../actions/quizzes";
import Navbar from "../layout/navbar";
class QuizDetail extends Component {
constructor(props) {
super(props);
this.state = {
currentStep: 0,
selectedOption: null,
questionId: null
};
}
static propTypes = {
user: PropTypes.object.isRequired,
quiz: PropTypes.object,
getQuiz: PropTypes.func.isRequired,
sendResponse: PropTypes.func.isRequired
};
onSubmit = e => {
e.preventDefault();
console.log("boulevard");
};
_setQuestion = questionId => {
console.log("me");
this.setState({ questionId });
};
handleOptionChange = changeEvent => {
this.setState({
selectedOption: parseInt(changeEvent.target.id, 10)
});
};
_next = answerId => {
let quiztaker = this.props.quiz.quiztakers_set[0].id;
let answer = this.state.selectedOption;
this.props.sendResponse(quiztaker, 1, 1);
let currentStep = this.state.currentStep;
let arrayLength = this.props.quiz.questions_count;
currentStep =
currentStep >= arrayLength - 1 ? arrayLength - 1 : currentStep + 1;
this.setState({
currentStep,
selectedOption: null
});
};
_prev = () => {
let currentStep = this.state.currentStep;
currentStep = currentStep <= 0 ? 0 : currentStep - 1;
this.setState({
currentStep
});
};
componentWillMount() {
this.props.getQuiz(this.props.match.params.slug);
}
render() {
if (!this.props.quiz) {
return <h1>LOADING...</h1>;
}
let icon_next =
this.state.currentStep === this.props.quiz.questions_count - 1
? "fas fa-check"
: "fas fa-angle-right";
let icon_prev =
this.state.currentStep === 0 ? "d-none" : "dir_control left";
return (
<Fragment>
<Navbar />
<div className="row question">
<div className="card col-lg-8 col-md-8 mx-auto">
{this.props.quiz.question_set.map((question, index) => {
let { currentStep } = this.state;
let style = {};
if (currentStep === index) {
() => this._setQuestion(question.id);
style = {
display: "inline-block"
};
} else {
style = {
display: "none"
};
}
return (
<div
className="card-body"
style={style}
key={question.id}
id={index}
>
<form onSubmit={this.onSubmit}>
<h5 className="card-title">{question.label}</h5>
{question.answer_set.map(answer => {
return (
<div className="answers" key={answer.id}>
<input
type="radio"
name={answer.question}
id={answer.id}
value={answer.text}
checked={this.state.selectedOption === answer.id}
onChange={this.handleOptionChange}
/>
<label htmlFor={answer.id}>{answer.text}</label>
<button
type="submit"
onClick={this._prev}
className={icon_prev}
>
<span className="fas fa-angle-left"></span>
</button>
<button
type="submit"
onClick={this._next}
className="dir_control right"
>
<span className={icon_next}></span>
</button>
</div>
);
})}
</form>
</div>
);
})}
</div>
</div>
</Fragment>
);
}
}
const mapStateToProps = state => ({
user: state.auth.user,
quiz: state.quizzes.quiz.quiz
});
export default connect(
mapStateToProps,
{ getQuiz, sendResponse }
)(QuizDetail);
每当我尝试不带任何参数时它都可以正常工作,但是当我添加参数时它根本不起作用。
使用 react 网站上的文档无济于事。我已经尝试了一切来完成这项工作。我也尝试了很长时间的堆栈溢出,直到我决定自己问这个问题。
P/S:我使用的是基于类的组件而不是功能组件
【问题讨论】:
-
您需要比“它不起作用”更具体一些 - 您是否收到错误消息?你想调用什么函数?您希望发生的事情不是?
-
我不认为它只是没有被调用!
-
再说一遍,什么功能?
-
在 if 语句中表示 this.DOSOMETHING 的函数。我希望它调用我在渲染之外定义的函数
-
我可以看到你试图在哪里调用它......我的意思是,它没有在示例中的任何地方声明 - 你在哪里声明
DOSOMETHING?