- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一款 Mad Libs 游戏,向用户提出如下问题:
Enter a Noun
用户填写正确的名称,点击提交,然后该文本输入现在被隐藏并显示下一个问题。
我知道它处理条件渲染以使其正常工作,但我不确定如何以及从哪里开始。我写了一个用户故事,试图在写的时候跟进,但仍然有点迷失。我的用户故事是
When user enters in text and hits submit (or the enter key) then the text input is hidden (but not deleted or removed because the value entered is used to pass props) and the next text input with a new input is revealed until. When all questions are answered, the Madlibs component is called.
除了上面的用户故事之外,一切都已构建完毕并正常运行。我现在没有收到错误,我只是不知道如何为此编写条件函数。
这是我的代码:
import React, { Component } from 'react';
import styled from 'styled-components';
import Crawler from './crawler';
const NextQuestion = styled.div`
position: absolute;
color: white;
display: block;
margin-top: 108px;
`;
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {
value1: 'enter proper name',
value2: 'noun',
value3: 'enter another proper name',
newValue: '',
submitted: false,
input1: 0,
input2: 0,
input3: 0,
input4: 0,
input5: 0,
input6: 0,
input7: 0
};
this.handleFirstChange = event => this.handleChange(event, 'value1');
this.handleSecondChange = event => this.handleChange(event, 'value2');
this.handleThirdChange = event => this.handleChange(event, 'value3');
this.handleFourthChange = event => this.handleChange(event, 'value4');
this.handleFifthChange = event => this.handleChange(event, 'value5');
this.handleSixthChange = event => this.handleChange(event, 'value6');
this.handleSeventhChange = event => this.handleChange(event, 'value7');
this.handleSubmit = event => this._handleSubmit(event);
}
handleChange(event, type) {
let newState = {};
newState[type] = event.target.value;
this.setState(newState);
}
_handleSubmit(event) {
event.preventDefault();
let toggle = this.state.visable;
this.setState({ visable: !toggle });
}
render() {
const divStyle = {
marginTop: '50px',
color: 'white',
top: '25px',
position: 'absolute'
};
let question = null;
const show = this.state.visable;
if (show) {
question = (
<div>
<Crawler
properName1={this.state.value1}
noun1={this.state.value2}
properName2={this.state.value3}
properName3={this.state.value4}
noun2={this.state.value5}
personsName1={this.state.value6}
noun3={this.state.value7}
/>
</div>
);
}
return (
<div>
<div style={divStyle}>
<form onSubmit={this.handleSubmit}>
<label>
Proper Name:
<input
name="input1"
type="text"
value={this.state.value1}
onChange={this.handleFirstChange}
/>
</label>
<label>
Noun:
<input
name="input2"
type="text"
value={this.state.value2}
onChange={this.handleSecondChange}
/>
</label>
<label>
Another Proper Name:
<input
name="input3"
type="text"
value={this.state.value3}
onChange={this.handleThirdChange}
/>
</label>
<label>
And Another Proper Name:
<input
name="input4"
type="text"
value={this.state.value4}
onChange={this.handleFourthChange}
/>
</label>
<label>
Noun:
<input
name="input5"
type="text"
value={this.state.value5}
onChange={this.handleFifthChange}
/>
</label>
<label>
Person's Name:
<input
name="input6"
type="text"
value={this.state.value6}
onChange={this.handleSixthChange}
/>
</label>
<label>
Another Noun:
<input
name="input7"
type="text"
value={this.state.value7}
onChange={this.handleSeventhChange}
/>
</label>
<input type="submit" value="Submit" />
</form>
</div>
<NextQuestion>
{question}
</NextQuestion>
</div>
);
}
}
export default NameForm;
我使用这个练习来教我更多关于 React 和条件的知识。感谢您的帮助。
最佳答案
首先您需要创建一个问题列表:数组
const QuestionsList = [
{
// Your question title
q: "Are you a human?",
// Validate the user answer
a: (answer) => (answer === 'yes'),
// Force user to give a correct answer
required: true
// Add more properties
...
},
...
];
您可以通过设置数组的索引
来浏览问题:
this.state = { current: 0 }
...
// Select current question
QuestionList[this.state.current]
// Select next question
QuestionList[this.state.current + 1]
// Select last question
QuestionList[QuestionList.length - 1]
When user enters in text and hits submit then the text input is hidden (but not deleted or removed because the value entered is used to pass props) and the next text input with a new input is revealed until.
由于您仅使用输入[text]
,因此您可以使用单个组件来解决所有问题,
无需在此处处理条件
:
const RenderQuestion = (
<div>
<h2>{question.q}</h2>
<input type="text"
value={this.state.value}
onChange={this.handleChange}
/>
<button onClick={this.handleSubmit}>
Submit
</button>
</div>
);
React 将通过 state
更新组件内容,
这会产生隐藏上一个/显示下一个
Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state
要解决此问题,请使用第二种形式的 setState
:
this.setState(prev => {
// Get previous state
const {value, current} = prev;
//If input not empty
if (value.length > 0) {
// Validate answer: true / false
const validation = questions[current].a(value);
//If not last question
return (prev.current < index) ?
//Select next question / reset input
{
current: prev.current + 1,
value:''
} :
// else: Completed!
{ completed: true };
}
});
参见:State Updates May Be Asynchronous
When all questions are answered, the Madlibs component is called.
{ this.state.completed ? <Madlibs /> : RenderQuestion }
If the condition is true, the element right after && will appear in the output. If it is false, React will ignore and skip it.
condition === true && element / method
Another method for conditionally rendering elements inline is to use the JavaScript conditional operator:
condition ? true : false
参见:Inline If with Logical && Operator
const QuestionsList = [
{
q: "What's your name?",
a: (answer) => /^[A-Za-z\s]+$/.test(answer),
// Force user to give a correct answer
required: true
},
{
q: "Type a color:",
a: (answer) => {
const colors = ['blue', 'green', 'red', 'yellow', 'black', 'brown', 'orange', 'pink', 'purple', 'white', 'gray']
return colors.includes(answer);
},
required: true
},
];
class Questionary extends React.Component {
constructor(props){
super(props);
//Init state
this.state = {
current: 0,
value:'',
completed: false,
data: []
};
// Bind handlers
this.handleSubmit = this.handleSubmit.bind(this);
this. handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit () {
const {questions} = this.props;
const index = questions.length - 1 ;
let state = {};
this.setState(prev => {
// Vars
const { value, current } = prev;
const question = questions[current];
const validation = question.a(value);
//If input not empty
if (value.length > 0) {
// Debug validation
!validation && console.log("Please enter a valid value!");
//Force user to give the correct answer)
if (question.required && validation == false) return state;
// Data for the template string
let data = prev.data;
data.push(value);
//If not last question
state = (prev.current < index) ?
//Select next question and reset input
{
current: prev.current + 1,
value:'',
data
} :
// else: Completed!
{ completed: true, data };
return state;
}
// Debug input
console.log("Empty input!");
});
}
render() {
// Get all questions
const { questions } = this.props;
//Select question
const question = questions[this.state.current];
// Question component
const RenderQuestion = (
<div>
<h2>{question.q}</h2>
<input type="text"
value={this.state.value}
onChange={this.handleChange}
/>
<button onClick={this.handleSubmit}>
Submit
</button>
</div>
);
// Score component
const RenderTemplates = (
<div>
<h2>Completed!</h2>
<p>
This is the story of
<span>{this.state.data[0]}</span>
and how the
<span>{this.state.data[1]}</span> dragon...
</p>
<p>
<span>{this.state.data[0]}</span> found a
<span>{this.state.data[1]}</span> goblin in the woods and then...
</p>
</div>
);
return (
<div>
{
this.state.completed ?
RenderTemplates : RenderQuestion
}
</div>
);
}
}
ReactDOM.render(<Questionary questions={QuestionsList}/>, document.getElementById("root"));
#root {
font-family: Arial, sans-serif;
color: #252525
}
h2, input[type=text] {
margin: 10px;
}
button {
border: 0;
border-radius: 3px;
background: #416dea;
padding: 5px 10px;
color: #FFF;
font-weight: bold;
border-radius: 3px;
}
p {
padding: 10px;
background: #ddd;
}
p span {
padding: 2px 4px;
margin: 0 10px;
background: #FFF;
color: #416dea;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.min.js"></script>
<div id="root"></div>
关于javascript - 提交后 React.js "hide element and reveal next element",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45400191/
我一直有同样的问题——在 Reveal.js 中创建一组垂直幻灯片后,使用嵌套 Content Content 该垂直列表之后的下一张幻灯片无法正常工作。我的代码编辑器无法将其识别为 HTML,它
我正在使用 reveal.js进行演示。我正在使用 white 主题: 但是,最后一张幻灯片的主题应该是black。 我能做到吗?怎么办? 最佳答案 执行此操作的一个好方法可能是像这样在最后一张幻灯
我需要在同一页面上有多个模式窗口。基本上我打算有一个指向每个团队成员的链接,然后单击后会在模式框中弹出他们的详细信息。我正在使用 zurb 的 Reveal 插件。这是网站上的示例。 html:
在 Powerpoint 中,单击鼠标左键可前进到下一张幻灯片。在 reveal.js 中,它是使用键盘完成的。是否可以配置 reveal.js 在单击鼠标按钮时也前进到下一张幻灯片? 最佳答案 看起
到目前为止,我很喜欢 Reveal.js。不过,我遇到的一个问题是,我的无线演示 Remote 通常只能生成左/右箭头键击。我的 Reveal.js 幻灯片大量使用了它的二维功能以及片段。这意味着我的
我当前正在表格上使用“全部显示”按钮来显示所有表格条目: http://jsfiddle.net/SCpgC/ 但是,我想知道,有没有办法可以在单击时将“全部显示”切换为显示“较少显示”,以便用户可以
当 reveal.js 文件夹与 markdown 文件无关时,我无法使用 Pandoc 制作独立的 reveal.js 幻灯片。 一个简单的 Markdown 文件,Tester.md: # Tit
演讲者备注适用于默认的 reveal.js 演示文稿。 在我的演示文稿中,只要我添加 notes.js 依赖项,演示文稿就会停止工作。 我不知道是什么导致了这个问题。我测试了将默认演示文稿 index
我正在使用 Reveal.initialize({ slideNumber: true }) 是否可以隐藏扉页上的幻灯片编号? 最佳答案 我是这样做的: Reveal.addEventList
我有一个使用reveal.js 运行的演示文稿,一切正常。我正在编写一些示例代码,并且 highlight.js 在我的演示文稿中运行良好。但是,我想逐步显示代码。例如,假设我正在向您解释一个功能,我
有了reveal.js,不仅可以水平滑动,还可以通过垂直滑动添加第二个维度: slide1 slide2 A slide2 B slide2 C 但是,似乎只能从顶部的第一个小节开始(
假设我有一个简单的 reveal.js像这样滑动: Title Text Fragment 我想换 Text后颜色变为红色Fragment出现在屏幕上。我该怎么做? 最佳答案
我用 reveal.js 设置了一个演示文稿这意味着在无人看管的情况下使用。所以,我希望能够在所有指向“信息幻灯片”的幻灯片上显示一个小页脚(或其他地方的另一个链接),其中包含有关作者的数据、如何使用
我正在尝试使用reveal.js 进行演示。我尝试将图像添加到幻灯片中,但图像不会显示。这是我的代码 Slide Header Symptom:
我正在尝试使用语法高亮显示 reveal.js 中的演示文稿,但我不想使用 zenburn(默认设置),我想高亮显示用 R 编写的代码。我使用 highlight.js 生成为 R 定制的 css,但
我正在使用 Xaringan 编写演示文稿。我想嵌入视频,这样当我转换到包含视频的幻灯片时,它会自动全屏播放。 (即,我不想必须点击视频才能播放)。 我可以使用 revealjs 实现我想要的: ##
我已将幻灯片编号添加到我的展示 js 演示文稿中 Reveal.configure({ slideNumber: true }); Reveal.configure({
我尝试只在一个部分更改字体颜色,因为我添加了背景图片,否则您无法阅读白色: blablabla
我正在学习使用reveal.js,我发现它非常强大。 我想知道是否有可能在每次幻灯片转换时使显示播放声音。 我做了一个启用自动滑动选项的演示文稿,当幻灯片更改时发出声音会很有用。 有人能帮我吗 ? 谢
reveal.js 中默认使用哪个主题来高亮语法? 最佳答案 默认主题是 Zenburn,参见 index.html . 也可以在这里尝试 Zenburn:http://softwaremaniacs
我是一名优秀的程序员,十分优秀!