4

I'm trying the new state management library from facebook recoil, I tried the Getting started example on a reactjs project and it worked perfectly. After that I tried recoil on a react-native project but I got an error:

Error message

Here's the code I've tried:

App.js

import React from 'react';
import {RecoilRoot} from 'recoil';
import RecoilTest from './RecoilTest';

const App = () => {
  return (
    <RecoilRoot>
      <RecoilTest />
    </RecoilRoot>
  );
};

export default App;

RecoilTest.js

import React from 'react';
import {useRecoilState} from 'recoil';
import {View, Text} from 'react-native';
import {textState} from './Atoms';

const RecoilTest = () => {
  const [text, setText] = useRecoilState(textState);
  return (
    <View>
      <Text>{text}</Text>
    </View>
  );
};

export default RecoilTest;

Atoms.js

import {atom} from 'recoil';

export const textState = atom({
  key: 'textState',
  default: 'initial value',
});
Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
Mahdi N
  • 2,128
  • 3
  • 17
  • 38

5 Answers5

3

Recoil don't has a fully support to react native yet

hackemate
  • 520
  • 4
  • 12
2

It is supported in nightly build. If you want to try before it is released with next version, you can install it doing:

yarn add https://github.com/facebookexperimental/Recoil.git#nightly

The update can be followed in this PR

Black
  • 9,541
  • 3
  • 54
  • 54
2

Towards release

See here

Hi everyone! FYI we've published a "nightly build" branch for testing purposes. If you want to try out recoil with react native before we release next version, try install the nightly branch with the "install git branch" feature of npm/yarn:

npm install https://github.com/facebookexperimental/Recoil.git#nightly

Or yarn add https://github.com/facebookexperimental/Recoil.git#nightly

Yoel
  • 7,555
  • 6
  • 27
  • 59
2

Update: RN support is now there in Recoil.js as Experimental.

https://github.com/facebookexperimental/Recoil/releases/tag/0.1.1

Yogesh Agrawal
  • 971
  • 10
  • 9
-2

Try using 'useRecoilValue' instead in your RecoilTest.js file

const [text, setText] = useRecoilValue(textState);