我定义了扩展react.component的类-并且添加了一些成员对象。 我想知道是否可以通过myContext函数访问_myClassMember(具有正确的上下文)吗?
{我不想通过在状态上定义_myClassMember来做到这一点}
class myClass extends React.Component
{
_myClassMember = null;
constructor(props)
{
super(props);
}
myContext(e)
{
e.preventDefault();
// here i want to have access somehow to _myClassMember;
}
// after add the img
render()
{
_myClassMember = 8;
const itemToRender =(<div>
<button onContextMenu={this.myContext}>
"MyButton"
</button>
</div>);
return itemToRender;
}
}
答案 0 :(得分:1)
您应在 <input id="ppleadd" type="checkbox" class="get_value" data-id="'.$ProductDetails->product_id.'" >
$(document).ready(function(){
$('.testbtn').on("click", function (){
var insert=[];
$('.get_value').each(function(){
if($(this).is(":checked"))
{
insert.push($(this).attr('data-id'));
}
});
insert=insert.toString();
$.ajax({
url:"add-to-cart",
method:"get",
data:{insert:insert},
success:function(data){
} }); }); });
中定义_myClassMember
并将其绑定到constructor
:
this
class App extends React.Component {
constructor(props) {
super(props);
this._myClassMember = null;
}
myContext = e => {
e.preventDefault();
// Access _myClassMember using this._myClassMember
console.log(this._myClassMember);
}
// after add the img
render() {
this._myClassMember = 8;
const itemToRender = (
<div>
<button onContextMenu={this.myContext}>"MyButton"</button>
</div>);
return itemToRender;
}
}
ReactDOM.render( < App / > , document.getElementById('root'));