javascript - React useContext from async service with Promise - Stack Overflow

I want to create a context that is actually ing from some async service (server data for example)..th

I want to create a context that is actually ing from some async service (server data for example)..

//the async service to bring the context data
export const fetchContextFromAsycnService = () => {
  return  new Promise(resolve => setTimeout(
      () => resolve('Hey, im in the async context and I am the correct value'), 200)
  );
} 

class App extends Component {
  render() {
    let value = 'Im not the correct value';
    fetchContextFromAsycnService().then(x => value = x as string);
    return (
      <AsyncContext.Provider value={value}>
       <SomeComponent />
      </AsyncContext.Provider>
    );
  }
}
//user the value
export const SomeComponent = ( ) => {
  return  (
    <AsyncContext.Consumer>
     {value => <div>{value}</div>}
  </AsyncContext.Consumer>)
}
render(<App />, document.getElementById('root'));

.tsx

The expected value is: Hey, I'm in the async context and I am the correct value, but for some reason im not getting the data after it was fetched.
is there a way to create a context with

I want to create a context that is actually ing from some async service (server data for example)..

//the async service to bring the context data
export const fetchContextFromAsycnService = () => {
  return  new Promise(resolve => setTimeout(
      () => resolve('Hey, im in the async context and I am the correct value'), 200)
  );
} 

class App extends Component {
  render() {
    let value = 'Im not the correct value';
    fetchContextFromAsycnService().then(x => value = x as string);
    return (
      <AsyncContext.Provider value={value}>
       <SomeComponent />
      </AsyncContext.Provider>
    );
  }
}
//user the value
export const SomeComponent = ( ) => {
  return  (
    <AsyncContext.Consumer>
     {value => <div>{value}</div>}
  </AsyncContext.Consumer>)
}
render(<App />, document.getElementById('root'));

https://stackblitz./edit/react-ts-8zzbxa?file=index.tsx

The expected value is: Hey, I'm in the async context and I am the correct value, but for some reason im not getting the data after it was fetched.
is there a way to create a context with

Share Improve this question edited Oct 4, 2019 at 6:10 Joseph D. 12.2k4 gold badges39 silver badges71 bronze badges asked Oct 4, 2019 at 5:56 SexyMFSexyMF 11.2k35 gold badges118 silver badges221 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

but for some reason im not getting the data after it was fetched.

value is not part of the state and does not cause a re-render. Thus, you haven't seen the updated value being displayed after fetch.

To make this work, just make value part of the state to cause a re-render.

state = {
  value: 'Im not the correct value'
}

ponentDidMount() {
  // move side-effects in here
  fetchContextFromAsycnService().then(value => this.setState({ value }));
}

render() {
  return (
    <AsyncContext.Provider value={this.state.value}>
      <SomeComponent />
    </AsyncContext.Provider>
  );
}

See Demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744168913a4561437.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
能,也可根据模型 hook 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>