Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应-如何访问数据集值_Javascript_Reactjs - Fatal编程技术网

Javascript 反应-如何访问数据集值

Javascript 反应-如何访问数据集值,javascript,reactjs,Javascript,Reactjs,我正在绘制年份列表(201720182019等) 我正在尝试console.log年值 <a href="#" onClick={() => console.log(event.target.getAttribute('data-year'))} data-year={year}>{year}</a> 有什么想法吗 您发布的代码似乎有输入错误——您的console.log没有右括号。此外,您实际上不需要在此处使用event.target.value。请查看

我正在绘制年份列表(201720182019等)

我正在尝试console.log年值

   <a href="#" onClick={() => console.log(event.target.getAttribute('data-year'))} data-year={year}>{year}</a>


有什么想法吗

您发布的代码似乎有输入错误——您的console.log没有右括号。此外,您实际上不需要在此处使用event.target.value。请查看:

<a href="#" onClick={ () => console.log(year) }>{year}</a>


当React创建
元素时,它知道您的年份变量是什么,并可以在console.log函数中使用它。

尝试使用数据属性和
事件.target.dataset.value

class ControlText extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      value: ""
    };
  }

  update(event) {
    console.log(event);
    this.setState({value: event.target.dataset.value});
  }

  render() {
    console.log(this.state);
    var value = this.state.value;
    return <a type="text" data-value={2017} onClick={this.update.bind(this)} > 2017 </a>
  }
}

ReactDOM.render(
  <ControlText />,
  document.getElementById('container')
);
类ControlText扩展React.Component{
建造师(道具){
超级(道具);
此.state={
值:“”
};
}
更新(事件){
console.log(事件);
this.setState({value:event.target.dataset.value});
}
render(){
console.log(this.state);
var值=this.state.value;

返回将
()
更改为
(事件)
。仍然不走运:-/你能把这作为一个答案,你应该得到分数:)谢谢你的帮助,但你的例子对我不起作用
class ControlText extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      value: ""
    };
  }

  update(event) {
    console.log(event);
    this.setState({value: event.target.dataset.value});
  }

  render() {
    console.log(this.state);
    var value = this.state.value;
    return <a type="text" data-value={2017} onClick={this.update.bind(this)} > 2017 </a>
  }
}

ReactDOM.render(
  <ControlText />,
  document.getElementById('container')
);