1

Is there any way to load separate sets of snippets depending on if I'm developing, for example, a react project vs. a react native project?

Basically, when I hit my keybinding (ctrl+shift+b), it should fill out my Button snippet, which is different for each:

react: "<button className=''>$1</button>",

react-native: "<Button title='$1' onPress={()=>{}}/>",

Right now I have a silly workaround where I use .jsx for web components and .js for react-native components, then configure my snippets for each language-mode accordingly, but I would prefer to use .jsx for both.

1 Answer 1

2

This explanation regards to all file types/snippets, the examples are of adding react snippets.

In VSCode, snippets are loaded according to file association. You can check the current file association on the bottom bar.

Example for JavaScript React file: enter image descriptionhere

Now you need to configure the snippet to the desired file type, for this you need to add files.associations property in settings.json.

File -> Preferences -> Settings -> Open Settings (JSON)

  "files.associations": {
    "*.react.js": "javascriptreact",
    "*.stories.js": "javascriptreact",
    "*.action.js": "javascriptreact",
    "*.reducer.js": "javascriptreact",
    "*.styles.js": "javascriptreact",
    "*.jsx": "javascriptreact",
    "*.js": "javascript"
  }

From this point, files names like MyComponent.react.js will associate with javascriptreact type.

All is left adding your snippets to javascriptreact type:

View -> Command Palette -> Preference: Configure User Snippets

and choose a file type (if exists like javascriptreact.json or create a new file type like javascriptreactnative).

All snippets in the chosen type will associate and auto-suggests.

For example, the next snippet in javascriptreact.json will work only in React files as configured above:

  "React Fragment": {
    "prefix": "<>",
    "body": [
      "<>",
      "$1",
      "</>"
    ],
    "description": "Insert React Fragment"
  }

In conclusion, you need to separate .jsx extension to recognize react and react-native, for example: *.native.jsx

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, thanks for the thorough answer Dennis! So if I'm understanding this right, there is no way to have snippets in a file with the exact same name (MyComponent.jsx) behave differently based on whether it is a react or a react native project? It can only be achieved by naming my react files and my react native files differently (beit through file extensions or some sort of file tail)?
Yes, like *.native.jsx for react-native files, its a good practice to use extensions like this for a clue of what the files hold.
Thanks a lot for your time! Learned a lot from your file associations example :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.