【发布时间】:2018-01-29 04:25:02
【问题描述】:
我有一系列关于我正在构建的注册流程的问题。目前,我正在遍历每个组件并将它们全部显示在一个页面上。我的问题是,如何一次只显示一个?当每张幻灯片隐藏/显示时,如何包含幻灯片向左过渡/动画?我希望每个问题单独显示,然后一旦用户单击下一步,它会隐藏第一个问题并显示第二个问题。我对 React 有点新,所以如果这是一个基本问题,我很抱歉,但我无法弄清楚。
以下是我的代码的突破:
import React from 'react';
import Q1Name from './questions/Q1Name';
import Q2Birthday from './questions/Q2Birthday';
import Q3City from './questions/Q3City';
import Q4YouReady from './questions/Q4YouReady';
import Q5Setting from './questions/Q5Setting';
import Q6Length from './questions/Q6Length';
import Q7Email from './questions/Q7Email';
class SignUpPage extends React.Component {
render() {
const components = [Q1Name, Q2Birthday, Q3City, Q5Setting, Q6Length, Q7Email];
const componentsToRender = components.map((Component, i) => (
<Component key={i} />
));
return (
<div className = "container-fluid">
<div className = "question-box">
{componentsToRender}
</div>
</div>
);
}
}
export default SignUpPage;
这是一个示例组件 - 它们都略有不同,所以我展示了两种主要类型:
第一个只有一个“下一步按钮”
import React from 'react';
class Q2Birthday extends React.Component {
render() {
return (
<div className="questions">
<h1 id="question-h1">When is your birthday?</h1>
<form>
<div className="form-group">
<input type="date" className="form-control custom-form" id="birthdayInput" aria-describedby="birthday" placeholder="" />
</div>
<button type="submit" className="btn btn-custom btn-lg">Next Question!</button>
</form>
</div>
);
}
}
export default Q2Birthday;
第二个有 3 个按钮选项供用户选择
import React from 'react';
class Q6Length extends React.Component {
render() {
return (
<div className="questions">
<h1 id="question-h1">How long would you like your trip to be?</h1>
<button type="submit" className="btn btn-custom-select btn-lg">Just a weekend!</button>
<button type="submit" className="btn btn-custom-select btn-lg">A full week!</button>
<button type="submit" className="btn btn-custom-select btn-lg">I'm flexible!</button>
</div>
);
}
}
export default Q6Length;
我还想为问题框类中的“问题”div 添加一个向左滑动的过渡。我一直在阅读 react-transition-group 但我对如何实现它有点困惑。另外,有了这个应用程序,我不需要存储表单数据的值。
【问题讨论】:
-
你为什么不玩 CSS 呢?只需添加/删除用于显示/隐藏组件的下一个/上一个按钮上的元素的类
-
我一直在玩 css 部分,但我很难让它们通过循环显示和隐藏。现在一切都在一次显示,所以我试图弄清楚如何使每个元素仅在单击前一个按钮的下一个按钮后才显示。
标签: javascript reactjs react-router react-redux css-transitions