javascript - passing parameter value to a jquery function from React code -
i have state variable declared as:
this.state = { userid: this.props.uid } we need adapt jquery command can receive this.state.userid inside react function. there general way this?
simples:
react
render() { return (<div id="user" data-userid={this.state.userid}></div>); } jquery
$("#user").data('userid'); then need victory dance because reasons. (<= important)
following our comments, getting user data via ajax react using fetch
componentwillmount = async () => { const response = await fetch(`users/${this.state.userid}`, {authentication: `bearer ${authtoken}`, 'content-type': 'application/json'}); if (response.status ===200) { //do better checks , stuff try{ const user = await response.json(); this.setstate({ user: user, }); } catch(e){ console.error('error response: '+e.message); } } else { console.error('response status '+response.status); } } i'd little more robust general idea. if you've never used fetch before i'd recommend looking cors , can right pain!
also if you're looking lot, lot more organised , 'app like' i'd looking redux, redux saga, redux persist , redux form. great packages!! anyway. moral of story is: kill jquery fire , stick react (y).
Comments
Post a Comment