javascript-React this.props为null
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-React this.props为null,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3315字,纯文字阅读大概需要5分钟。
内容图文
![javascript-React this.props为null](/upload/InfoBanner/zyjiaocheng/667/ab294091750243bab41a223782d38cf8.jpg)
我有以下内容:
import React from 'react';
import {render} from 'react-dom';
class TShirt extends React.Component {
render () {
return <div className="thsirt">{this.props.name}</div>;
}
}
class FirstName extends React.Component {
propTypes: {
name: React.PropTypes.string.isRequired
}
constructor(props) {
super(props);
this.state = {
submitted: false
};
}
getName () {
var name = this.refs.firstName.value;
this.setState(function() {
this.props.action(name);
});
}
handleSubmit (e) {
e.preventDefault();
this.setState({ submitted: true }, function() {
this.props.actionNav('color');
});
}
render () {
if(!this.state.submitted){
return (
<div>
<h2>tell us your first name</h2>
<form>
<input
type="text"
ref="firstName"
onChange={this.getName.bind(this)}
/>
<div className="buttons-wrapper">
<a href="#">back</a>
<button onClick={this.handleSubmit.bind(this)}>continue</button>
</div>
</form>
</div>
);
}
else {
return <PickColor color={this.props.colorVal} />;
}
}
}
class Link extends React.Component {
setActiveClass () {
if(this.props.el == this.props.activeClass){
return 'active';
}
}
render () {
var active = this.setActiveClass();
return (
<li className={active}>{this.props.el}</li>
);
}
}
class Nav extends React.Component {
render () {
var links = ['name', 'color', 'design', 'share'],
newLinks = [],
that = this;
links.forEach(function(el){
newLinks.push(<Link activeClass={that.props.active} key={el} el={el} />);
});
return (
<ul>
{newLinks}
</ul>
);
}
}
class PickColor extends React.Component {
getColorValue(event) {
console.log(event.target.getAttribute("data-color"));
console.log(this);
//this.props.color(event.target.getAttribute("data-color"));
}
render () {
var colors = ['red', 'purple', 'yellow', 'green', 'blue'],
colorsLink = [],
that = this;
colors.forEach(function(el){
colorsLink.push(<li data-color={el} key={el} onClick={that.getColorValue} ref={el}>{el}</li>);
});
return (
<ul>
{colorsLink}
</ul>
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
name: '',
color: '',
active: ''
};
this.getName = this.getName.bind(this);
this.setActiveNav = this.setActiveNav.bind(this);
this.setColor = this.setColor.bind(this);
}
getName (tshirt) {
this.setState({ name:tshirt })
}
setActiveNav (active) {
this.setState({ active:active })
}
setColor (color) {
this.setState({ color:color })
}
render () {
return (
<section className={this.state.color}>
<Nav active={this.state.active} />
<TShirt name={this.state.name} />
<FirstName name={this.state.name} action={this.getName} actionNav={this.setActiveNav} colorVal={this.setColor} />
</section>
);
}
}
render(<App/>, document.getElementById('app'));
在“ PickColor”组件中,我正在尝试执行以下操作:
getColorValue(event) {
console.log(event.target.getAttribute("data-color"));
console.log(this);
//this.props.colorVal(event.target.getAttribute("data-color"));
}
但这在点击时返回null,所以我不能继续使用:
this.props.colorVal
解决方法:
解决方案在这里:
class PickColor extends React.Component {
getColorValue(event) {
console.log(event.target.getAttribute("data-color"));
this.props.color(event.target.getAttribute("data-color"));
}
render () {
var colors = ['red', 'purple', 'yellow', 'green', 'blue'],
colorsLink = [],
that = this;
colors.forEach(function(el){
colorsLink.push(<li data-color={el} key={el} onClick={that.getColorValue.bind(that)} ref={el}>{el}</li>);
});
return (
<ul>
{colorsLink}
</ul>
);
}
}
我必须将“ that”绑定到forEach循环内的onClick
内容总结
以上是互联网集市为您收集整理的javascript-React this.props为null全部内容,希望文章能够帮你解决javascript-React this.props为null所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。