Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # create-react-context
  2. > Polyfill for the [proposed React context API](https://github.com/reactjs/rfcs/pull/2)
  3. ## Install
  4. ```sh
  5. yarn add create-react-context
  6. ```
  7. You'll need to also have `react` and `prop-types` installed.
  8. ## API
  9. ```js
  10. const Context = createReactContext(defaultValue);
  11. // <Context.Provider value={providedValue}>{children}</Context.Provider>
  12. // ...
  13. // <Context.Consumer>{value => children}</Context.Consumer>
  14. ```
  15. ## Example
  16. ```js
  17. // @flow
  18. import React, { type Node } from 'react';
  19. import createReactContext, { type Context } from 'create-react-context';
  20. type Theme = 'light' | 'dark';
  21. // Pass a default theme to ensure type correctness
  22. const ThemeContext: Context<Theme> = createReactContext('light');
  23. class ThemeToggler extends React.Component<
  24. { children: Node },
  25. { theme: Theme }
  26. > {
  27. state = { theme: 'light' };
  28. render() {
  29. return (
  30. // Pass the current context value to the Provider's `value` prop.
  31. // Changes are detected using strict comparison (Object.is)
  32. <ThemeContext.Provider value={this.state.theme}>
  33. <button
  34. onClick={() => {
  35. this.setState(state => ({
  36. theme: state.theme === 'light' ? 'dark' : 'light'
  37. }));
  38. }}
  39. >
  40. Toggle theme
  41. </button>
  42. {this.props.children}
  43. </ThemeContext.Provider>
  44. );
  45. }
  46. }
  47. class Title extends React.Component<{ children: Node }> {
  48. render() {
  49. return (
  50. // The Consumer uses a render prop API. Avoids conflicts in the
  51. // props namespace.
  52. <ThemeContext.Consumer>
  53. {theme => (
  54. <h1 style={{ color: theme === 'light' ? '#000' : '#fff' }}>
  55. {this.props.children}
  56. </h1>
  57. )}
  58. </ThemeContext.Consumer>
  59. );
  60. }
  61. }
  62. ```
  63. ## Compatibility
  64. This package only "ponyfills" the `React.createContext` API, not other
  65. unrelated React 16+ APIs. If you are using a version of React <16, keep
  66. in mind that you can only use features available in that version.
  67. For example, you cannot pass children types aren't valid pre React 16:
  68. ```js
  69. <Context.Provider>
  70. <div/>
  71. <div/>
  72. </Context.Provider>
  73. ```
  74. It will throw `A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.` because `<Context.Provider>` can only receive a single child element. To fix the error just wrap everyting in a single `<div>`:
  75. ```js
  76. <Context.Provider>
  77. <div>
  78. <div/>
  79. <div/>
  80. </div>
  81. </Context.Provider>
  82. ```