[英]onClick of button increment ID on url with React.js
I'm trying to increment the number I get from this.props.match.params.id
to get the DOM to re-render with new API fetched content. 我正在尝试增加从
this.props.match.params.id
获得的this.props.match.params.id
以使DOM用新的API提取的内容重新呈现。 I was trying to do that by calling a function on Click and incremeting value by one. 我试图通过在Click上调用一个函数并将值增加1来实现此目的。 However, that's not happening at all, instead of incrementing the value, it concatenates like this:
但是,这根本没有发生,而是像这样串联:
https://imgur.com/a/dnKScN0 https://imgur.com/a/dnKScN0
This is my code: 这是我的代码:
export class Sofa extends React.Component {
constructor(props) {
super(props);
this.state = {
token: {},
isLoaded: false,
model: {}
};
}
addOne = () => {
console.log('addOne');
this.props.match.params.id += 1;
console.log('id: ', this.props.match.params.id);
}
lessOne = () => {
console.log('lessOne');
}
componentDidMount() {
/* fetch to get API token */
fetch(url + '/couch-model/' + this.props.match.params.id + '/', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'JWT ' + (JSON.parse(localStorage.getItem('token')).token)
}
}).then(res => {
if (res.ok) {
return res.json();
} else {
throw Error(res.statusText);
}
}).then(json => {
this.setState({
model: json,
isLoaded: true
}, () => { });
})
}
render() {
const { model, isLoaded } = this.state;
if (!isLoaded) {
return (
<div id="LoadText">
Loading...
</div>
)
} else {
return (
<div id="Sofa">
/* other HTML not important for this matter */
<img src="../../../ic/icon-arrow-left.svg" alt="Arrow pointing to left" id="ArrowLeft"
onClick={this.lessOne}/>
<img src="../../../ic/icon-arrow-right.svg" alt="Arrow pointing to right" id="ArrowRight"
onClick={this.addOne}/>
</div>
);
}
}
}
You can't manipulate props, they are immutable. 您无法操纵道具,它们是不可变的。 You have a few options:
您有几种选择:
As your string is concatenating JS thinks its a string and not a number. 当您的字符串串联时,JS认为它是字符串而不是数字。 Just increment like so
Number(this.props...id) + 1
像这样递增
Number(this.props...id) + 1
Very likely this.props.match.params.id
is a string. this.props.match.params.id
很可能是字符串。 Doing this: '1'+1
returns '11'
. 这样做:
'1'+1
返回'11'
。
It seems that this.props.match.params.id
is string. 看来
this.props.match.params.id
是字符串。 Adding a value to string
variable works as concatination. 向
string
变量添加值可作为隐含条件。
You need to parseInt
before adding a value like: 您需要先
parseInt
然后再添加类似以下内容的值:
this.props.match.params.id = parseInt(this.props.match.params.id) + 1;
I guess this is a 'main' component - just 'below' router. 我猜这是一个“主要”组件-只是在“下方”路由器。 You can't modify param from router - you should generate link to next item (pointing the same component) which will get it's own props ('updated' id) from router.
您无法通过路由器修改参数-您应该生成指向下一项的链接(指向相同的组件),该链接将从路由器获取其自身的道具(“更新的” ID)。
You're sure you will have contiuous range of ids? 您确定您将拥有连续的ID范围吗? No deletions in db?
数据库中没有删除? Rare ;)
稀有;)
Normally you have a searched/category based list of items. 通常,您有一个基于搜索/类别的项目列表。 Browsing list ... entering item (by id), back to list, enter another item.
浏览列表...输入项目(按ID),返回列表,输入另一个项目。 Browsing items w/o info about list is a little harder and inc/dec id isn't good idea.
没有清单信息的浏览项目会有点困难,而inc / dec id并不是一个好主意。
Get list into main component, pass current id to sub-component (able to load data itself and render it). 将列表获取到主要组件中,将当前ID传递给子组件(能够加载数据本身并呈现数据)。 Prepare next/prev buttons in main (modify current in state, loop them?) - changed prop (id) will force rendering component to load next data.
在主屏幕中准备下一个/上一个按钮(修改状态下的电流,是否将其循环?)-更改后的prop(id)将强制渲染组件加载下一个数据。
Further ... extract next/prev into sub-components, passing handlers from main (you didn't passed them and even if it won't work for reasons above). 进一步...将next / prev提取到子组件中,从main传递处理程序(您没有传递它们,即使由于上述原因而无法使用)。
Accessing items only by id is a bit harder, fe you can 'ask' api for next/prev items for current one. 仅通过id访问项目会有点困难,例如,您可以为当前项目的下一个/上一个项目“询问” api。 It can be done async - render next/prev when data will be ready.
可以异步完成-在数据准备就绪时呈现next / prev。
Don't make all in one step ;) 不要一all而就;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.