��权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - React radio button onChange event does not bind on first change of radio button - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React radio button onChange event does not bind on first change of radio button - Stack Overflow

programmeradmin3浏览0评论

I am working with react js and found a weird issue on radio button onChange event binding. My page opens a popup on button click where a new ponent bind inside that popup. In this new ponent I have created 2 radio buttons and on change of that radio buttons I'm hide/show div. below is my code.

 class ponent1 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showComponent: true
   };
  }
  handleChange = () => {
    this.setState({
      showComponent: !this.state.showComponent,
    });
  }
  render() {
   return(
   <div>
    <input type='radio' name='a' onChange={self.handleChange} defaultChecked/>
    <input type='radio' name='a' onChange={self.handleChange}/>
     {this.state.showComponent && (<div>Hide or show based on state change</div>)}
   </div>
  );
 }
}

When I open the popup for the first time it works fine. Perhaps its behavior change after submitting form and popup close. When next time I open popup without parent page refresh, on first change of radio button it does not call handleChange function. And from the second click it just works fine.

I think, onSubmit I have called form.reset() function on successful submission of form, which is creating problem. But i don't understand how to resolve this issue.

I am working with react js and found a weird issue on radio button onChange event binding. My page opens a popup on button click where a new ponent bind inside that popup. In this new ponent I have created 2 radio buttons and on change of that radio buttons I'm hide/show div. below is my code.

 class ponent1 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showComponent: true
   };
  }
  handleChange = () => {
    this.setState({
      showComponent: !this.state.showComponent,
    });
  }
  render() {
   return(
   <div>
    <input type='radio' name='a' onChange={self.handleChange} defaultChecked/>
    <input type='radio' name='a' onChange={self.handleChange}/>
     {this.state.showComponent && (<div>Hide or show based on state change</div>)}
   </div>
  );
 }
}

When I open the popup for the first time it works fine. Perhaps its behavior change after submitting form and popup close. When next time I open popup without parent page refresh, on first change of radio button it does not call handleChange function. And from the second click it just works fine.

I think, onSubmit I have called form.reset() function on successful submission of form, which is creating problem. But i don't understand how to resolve this issue.

Share Improve this question edited Jul 1, 2022 at 18:41 Jonas 129k102 gold badges327 silver badges405 bronze badges asked Nov 2, 2018 at 16:16 Code_ArtCode_Art 1211 gold badge1 silver badge11 bronze badges 4
  • It's hard to say what might be wrong from the code currently in your question. It would be helpful if you could create a Minimal, Complete, and Verifiable example in e.g. CodeSandbox. – Tholle Commented Nov 2, 2018 at 16:21
  • Perhaps it makes sense to make handleChange accept an argument, and do the toggling depending on that argument? Can't the value of radio buttons be 'indefinite' after you call 'form.reset()` I wonder? – raina77ow Commented Nov 2, 2018 at 16:23
  • probably the edit but self.handleChange and not this.handleChange which is arrow bound? you've not declared self = this – Dimitar Christoff Commented Nov 2, 2018 at 16:24
  • On handlechange (in sestate block) use the prevstate to get value of showp. – benchpresser Commented Nov 2, 2018 at 17:18
Add a ment  | 

1 Answer 1

Reset to default 5
<input type='radio' name='a' onChange={this.handleChange} checked={!this.state.showComponent} />
<input type='radio' name='a' onChange={this.handleChange} checked={this.state.showComponent}/>

this will work find in a controlled way as opposed to reliance on event and defaultChecked

https://codesandbox.io/s/5z40rj836l

发布评论

评论列表(0)

  1. 暂无评论