- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
父组件有这个
value={this.onUpdate(index)}
onUpdate 会处理值和索引
在子组件中我有一个输入字段
onChange={this.handleText(index)}
这会调用一个想要将 Prop 发回的方法
this.props.value(sum);
我认为我需要为值(value)做界面,但是怎么做?
编辑:家长:
import * as React from "react";
import styled from 'styled-components';
import ShoppingList from './ShoppingList';
const Container = styled.div`
position:absolute;
left:0;
bottom:0;
right:0;
display: flex;
flex-direction: row;
justify-content: space-between;
`;
interface Item {
id: number;
value: number;
}
interface TPState {
shoppingLists2: object[];
shoplistsums: Item[];
sum: number;
}
class TotalPrice extends React.Component<{}, TPState> {
constructor(props: {}) {
super(props);
this.state = {
shoppingLists2: [ShoppingList],
sum: 0,
shoplistsums: []
}
}
AddShoppingList = () => {
let shoppingLists2 = this.state.shoppingLists2.concat(ShoppingList);
console.log(shoppingLists2)
this.setState({
shoppingLists2,
})
}
onUpdate = index => (value) => {
console.log('index, value', index, value)
let item: Item = {
id: index,
value: value
}
let shoplistsums = [...this.state.shoplistsums];
var indexOfItem = shoplistsums.map(e => e.id).indexOf(item.id);
if (indexOfItem === -1) {
shoplistsums.push(item);
}
else {
shoplistsums[indexOfItem] = item;
}
let sum = shoplistsums.map(this.amount).reduce(this.sum);
this.setState({
shoplistsums,
sum
})
}
amount(item) {
return item.value;
}
sum(prev, next) {
return prev + next;
}
render() {
return (
<div>
{this.state.shoppingLists2.map((ShoppingList, index) => (
<ShoppingList
key={index}
value={this.onUpdate(index)}
/>
))}
<Container>
<label>Total:</label>
<label>{this.state.sum + ' €'}</label>
<button onClick={this.AddShoppingList}>Add Receipt</button>
</Container>
</div>
);
}
}
export default TotalPrice;
child
import * as React from "react";
import styled from 'styled-components';
const Container = styled.div`
display: flex;
flex-direction: row;
justify-content: space-between;
`;
export interface SLProps {
}
interface SLState {
smallList: number[];
sum: number;
}
export default class ShoppingList extends React.Component<SLProps, SLState> {
constructor(props: SLProps) {
super(props);
this.state = {
smallList: [0],
sum: 0
}
}
handleText = i => e => {
let smallList = [...this.state.smallList];
let x = e.target.value;
if (isNaN(parseFloat(x))) {
smallList[i] = 0;
}
else {
smallList[i] = parseFloat(x);
}
let sum = smallList.reduce(function(a, b) {
return a + b;
});
this.props.value(sum);
this.setState({
smallList,
sum
})
}
addExpense = e => {
e.preventDefault()
let smallList = this.state.smallList.concat([0])
this.setState({
smallList
})
}
render() {
return (
<div>
<Container>
<select>
<option value="food">Food</option>
<option value="houseware">Houseware</option>
<option value="entertainment">Entertainment</option>
</select>
<button onClick={this.addExpense}>Add expense</button>
</Container>
{this.state.smallList.map((question, index) => (
<Container key={index}>
<input
type="text"
/>
<input
type="number"
step="0.01"
onChange={this.handleText(index)}
value={question === 0 ? '' : question}
/>
</Container>
))}
<Container>
<label>Total:</label>
<label>{this.state.sum + ' €'}</label>
</Container>
</div>
)
}
}
最佳答案
export interface SLProps {
value: (value: number) => void
}
关于javascript - 如何在 TSX 中将值从子组件传递给父组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51045538/
我正在自学在 React Native 中使用 TypeScript 构建应用程序。作为一名 Swift 开发人员,JS 和 TS 需要一点点习惯。 我注意到的一件事是,似乎不可能在 Render 方
我们都知道,通常情况下我们使用 vue 大多都是用的 SFC(Signle File Component)单文件组件模式,即一个组件就是一个文件,但其实 Vue 也是支持使用 JSX 来编写组件的。
2-3 周,虽然我没有更改漂亮文件中的任何内容,但 VSC 开始将单引号更改为双引号,而它始终是单引号。我尝试了很多选择,但它一直在改变它。这是我的 .prettierrc { "trailing
我有一个名为“test.ts”的示例文件,代码如下: const test = (a: string) => { return a; } ; 有效!如果我将文件重命名为“test.tsx”,则 Vis
我试图了解在 typescript 中使用 tsx 文件进行类型推断是否存在限制。 如果我创建一个无状态的 React 组件: interface TestProps { foo: strin
我正在尝试测试我的 typescript react 应用程序,并且我有 nodemon 在 .ts 文件上运行 jasmine 测试,但我无法让 .tsx 触发 watch Action 。 我的
我想评论我在下面显示的 TSX 代码。我该怎么做? Type 最佳答案 要在 JSX 中间添加评论,请使用大括号返回纯 javascript,然后添加您的评论: {/* Hello World
我使用 yarn v2 安装依赖包,并使用 yarn start命令可以顺利启动项目,但是vscode总是提醒我找不到任何本地模块。 这是我的 tsconfig.json文件: { "compil
这可能是一个简单的问题,但在网上找不到答案。在 ts文件我可以定义这样的函数: const lastGeneric = (arr: Array): T => { return arr[arr.l
我想在 Delphi 中使用 Intel TSX 同步扩展。但是 AFAIK Delphi 不支持 SSE 4.2 之后的任何扩展程序集,所以可以用其他方式完成吗?以及如何检测这样的特征。 Haswe
父组件有这个 value={this.onUpdate(index)} onUpdate 会处理值和索引 在子组件中我有一个输入字段 onChange={this.handleText(index)
我想利用 Intel TSX 编写无锁代码。 xbegin my_inst1 my_inst2 xend 但是,由于某些原因,我在 TSX 执行 TSX 中的一条指令中止。 我想知道哪条指令产生了故障
教程来源:有状态组件 - https://facebook.github.io/react/ 这是我的代码: interface Props { } interface State { sec
我正在查看 Live555 媒体服务器。它有一个用于“索引”传输流视频的可执行文件,其名称/用途如下: MPEG2TransportStreamIndexer video.ts 因此,如果视频名为 T
假设您有两个线程,一个创建一个 TSX 事务,并修改一些数据结构。另一个线程不执行任何类型的同步并读取相同的数据结构。交易对它来说是原子的吗?我实际上无法想象这是真的,因为如果它尝试读取由事务修改的缓
我在单独的文件中有一个常量 EmptyNode.tsx : export const EmptyNode = <> 当我不想显示任何内容时,我用它来返回一个空节点。 例子: ... render()
给定以下 JSX 代码: 如何注释掉 className="my-class"? /*className="my-class"*/ 不起作用 {/* className="my-class"*/}
Typescript 引入了对 JSX 语法的支持。所以我有一个表达式可以很好地处理传统的 *.ts 文件,但不能处理 *.tsx 文件: const f = (arg1: T1) => (arg2:
通常在我的 .ts 文件中,我可以通过调用以下内容来访问窗口对象: (window).myObject 我的 .tsx 文件中出现编译错误。有什么方法可以从 .tsx 文件访问它吗? 谢谢。 最佳答案
我是 Typescript 的新手,正在尝试为 TypeScript 构建一个 React 样板。但不能导入形式为“.tsx”的组件。 我的 tsconfig 文件如下所示: { "compile
我是一名优秀的程序员,十分优秀!