- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法弄清楚/概念化如何在 ReactJS 中创建组件。我拼凑了一个小例子,其中我有一个膳食计划的最重要组成部分,然后我有另一个组件用于计划中的每顿饭。
现在,我想要一个组件来显示用户选择膳食类别的次数。我在这里举了一个小例子。 Meal Plan Example
所以使用这个例子,我想要一个组件来总结用户当前选择了多少水果和蔬菜。我以前的想法是我需要在类别更改的某个地方更改计数,但我认为在 react 中这应该是不同的并且更容易,对吧?感觉也许我应该(以某种方式?)获取类别选择框的值并计算它们并将其呈现在新组件中。感觉也许我应该看看引用文档,但他们使用了一个非常简单的例子。
不太确定解决此问题的正确方法是什么。
这也是代码。
JSON
var plan = {
"meals": [
{
"selectedFood":"",
"mealName":"breakfast"
},
{
"selectedFood":"",
"mealName":"lunch"
},
{
"selectedFood":"",
"mealName":"dinner"
},
],
"planOptions": [
{
"id":1,
"category":"fruit",
"food":"apple"
},
{
"id":2,
"category":"fruit",
"food":"pear"
},
{
"id":3,
"category":"vegetable",
"food":"carrot"
},
{
"id":4,
"category":"vegetable",
"food":"corn"
}
]
};
JSX
var PlanEntryBox = React.createClass({
getInitialState: function() {
return {
plan: {}
};
},
render: function() {
var planOpts = this.props.plan.planOptions;
var meals = this.props.plan.meals.map(function(meal) {
return (
<MealEntryBox mealEntry={meal} planOptions={planOpts} />
);
});
return (
<div>
<h1>Meals</h1>
<table className="table">
<thead>
<tr>
<th>Meal</th>
<th>Category</th>
<th>Food</th>
</tr>
</thead>
<tbody>
{meals}
</tbody>
</table>
<CategoryCounter />
</div>
);
}
});
var MealEntryBox = React.createClass({
getInitialState: function() {
return {
selectedCategory: null
};
},
categoryChange: function(event) {
this.setState({selectedCategory: event.target.value});
},
render: function() {
var options = _.uniq(_.pluck(this.props.planOptions, 'category'));
var categoryOpts = options.map( function(category) {
return (
<option>{category}</option>
);
});
var filteredOptions = _.filter(this.props.planOptions, {"category": this.state.selectedCategory});
var foodOpts = filteredOptions.map( function(option) {
return (
<option>{option.food}</option>
);
});
return (
<tr>
<td>{this.props.mealEntry.mealName}</td>
<td>
<select className="form-control" onChange={this.categoryChange}>
<option>Pick one... </option>
{categoryOpts}
</select>
</td>
<td>
<select className="form-control" disabled={!this.state.selectedCategory}>
{foodOpts}
</select>
</td>
</tr>
);
}
});
var CategoryCounter = React.createClass({
render: function() {
return(
<div>
<h3>Counts</h3>
</div>
);
}
});
React.render(
<PlanEntryBox plan={plan} />,
document.getElementById('content')
);
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>
<script src="//fb.me/react-0.13.1.js"></script>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<title>Meal Plan Example</title>
</head>
<body>
<div id="content">
</div>
</body>
</html>
最佳答案
这是对组件 state
的相当直接的使用。
您需要在父组件(PlanEntryBox
)中存储水果和蔬菜的数量。从更新 PlanEntryBox.getInitialState()
开始,为这些计数器添加字段。
getInitialState: function() {
return {
plan: {},
fruitCount: 0,
vegieCount: 0
};
},
然后,向 PlanEntryBox
组件添加函数来处理更新计数:
addFruit: function(){
this.setState({fruitCount: this.state.fruitCount++});
},
addVegie: function(){
this.setState({vegieCount: this.state.vegieCount++});
}
(您也可能需要减少计数的方法,但我会把它留给您。)
然后将对这些方法的引用作为 Prop 传递给每个 MealEntryBox
组件。将 PlanEntryBox
行的 render
函数修改为:
<MealEntryBox mealEntry={meal} planOptions={planOpts}
onFruit={this.addFruit} onVegie={this.addVegie}/>
然后在 MealEntryBox.categoryChange
中使用 onFruit
和 onVegie
回调:
categoryChange: function(event) {
if(event.target.value === 'fruit'){
this.props.onFruit();
} else {
this.props.onVegie();
}
this.setState({selectedCategory: event.target.value});
},
(注意:同样,这是一个天真的实现。如果用户取消选择餐点或将类型从一个类别更改为另一个类别,您将需要处理递减值。)
例如调用onFruit
,将导致父组件更新其状态(使用addFruit()
中的setState()
调用) .这将导致 React 渲染组件,并将新的 props 传递给它的子元素。您可以通过将计数传递给 CategoryCounter
组件来利用这一事实。
在 PlanEntryBox
的主要 render()
函数中修改使用 CategoryCounter
的行,如下所示:
<CategoryCounter fruits={this.state.fruitCount} vegies={this.state.vegieCount} />
然后在 CategoryCounter
的渲染方法中,显示那些新的 Prop :
render: function() {
<div>
<h3>Counts</h3>
<ul>
<li>Fruits: {this.props.fruits}</li>
<li>Vegetables: {this.props.vegies}</li>
</ul>
</div>
}
策略总结:
state
存储计数CategoryCounter
,以便它可以显示计数CategoryCounter
,然后它也会重新渲染并显示其新 Prop 。要进一步阅读回调和向组件树向上传递数据,请参阅 React 教程的第五步:https://facebook.github.io/react/docs/thinking-in-react.html#step-5-add-inverse-data-flow
关于javascript - ReactJS 中选择输入的计数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31859353/
这是关于非标准属性的。 https://facebook.github.io/react/docs/tags-and-attributes.html 在 react 中我做了这个: React.cr
我制作了 2 个组件:1)内容(使用 fetch() 显示数据)2)分页(共5页,每页16个对象) 我创建了 2 个状态变量,分别称为 start:0 和 end:16。现在,每当用户单击页码时,我都
我们正在使用 ReactJS 创建一个搜索前端。要求是当用户单击文档名称时,该文档必须在嵌入式查看器中打开。有人为此使用过任何 ReactJS 组件吗?否则,我应该使用 JQuery 查看器组件吗?请
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 1 年前。
我现有的项目: 前端框架 - PHP Codeigniter 和 PHP Laravel 后端框架 - JAVA Springboot 我正在尝试将前端框架更改为 ReactJS。我想知道与 Reac
我有一个实现刷新授权 token 的方案。在这种情况下,我为每种操作(发布、获取、删除)实现了一个通用操作,并使用参数调用它们。 export function Get(param) { return
我现在有 { _this.setState({startDate: input})}}/> 无论我一直在改变什么(onChange 部分到 ref 等等),当我 console.log 我的状态时,
我在两个不同的文件中有两个单独的组件 组件A 和 组件B 我在 中有一个按钮组件B 现在我想测试当 中的特定按钮时组件B 被点击,组件A 应呈现如下: import { render, screen,
我有一个包含几个问题的表格。一些问题有一组子问题。 这些是使用以下代码呈现的。 { Object.keys(this.props.moduleDetails.questions).map(
我有两个组件。第一个状态初始化: import React from 'react'; class One extends React.Component { constructor(prop
我的项目是一个对象数组,我只获取名称并在 3 in 3 的屏幕上渲染,使用“下一个”和“上一个”按钮更改名称,并且可以过滤字母。 我想要添加一个新值,在输入中键入并单击按钮添加。 我的代码按钮: ad
我有一个组件实例,应该从不同的地方更新。如何从其他几个组件中更新此组件的 props? class ParentComponent extends React.Component { ...
我想问,是否可以基于变量返回一个组件,我的想法是否可以: var location = this.props.location // Eg. Asia,Australia 我的问题是我需要单独手动导
有没有办法在渲染后立即选择要覆盖的文本?我的意思是选择 - 最佳答案 您可以尝试使用两种方法.focus和.select .focus() method sets focus on the speci
我渲染了以下内容: 这是事件处理程序: handleCompanySubmit(event) { console.log("company submit") e
作为ReactJS的初学者,我现在使用出色的ReactJS Datepicker。有关我的自定义日期格式的问题: 我没有找到任何文档来解释所有可能性,例如M是月份,MM是2个字母的月份,依此类推。(我
我创建了一个表并正在对其进行过滤,但是当我清除搜索查询时,表数据不会显示,并且表会卡在查询结果上。SearchBox 是文本框,TableData 是表格,在 Container 中我正在更改 Sea
我在表格设置中有图标,这样当单击图标时,会呈现不同的图标。现在这工作正常,但我想为我单击的行中的特定行重新呈现不同的图标,而不是在每一行中重新呈现不同的图标。不知道我将如何去做这件事。这是我的代码:
我正在尝试使用 React 构建一个简单的应用程序,它有两个组件,一个嵌入另一个组件。子组件是一个收缩的菜单,单击它时,它会展开。我希望能够在单击父元素或子元素失去焦点时重新收缩它。 这是父组件的样子
我需要知道 REACT JS 或 HTML5 中是否有任何 API 可以提供用户不活动时自动注销的功能。我的代码如下,我不知道出了什么问题,它给了我错误 startTimer 未定义和不必要的绑定(b
我是一名优秀的程序员,十分优秀!