Source:  Twitter logo

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:

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',
});

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

3 users liked answer #0dislike answer #03
hackemate profile pic
hackemate

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

2 users liked answer #1dislike answer #12
Yoel profile pic
Yoel

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

2 users liked answer #2dislike answer #22
Black profile pic
Black

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

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

2 users liked answer #3dislike answer #32
Yogesh Agrawal profile pic
Yogesh Agrawal

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

const [text, setText] = useRecoilValue(textState);
-2 users liked answer #4dislike answer #4-2
divsoup profile pic
divsoup

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).