惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
IT之家
IT之家
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
W
WeLiveSecurity
B
Blog RSS Feed
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
S
Security @ Cisco Blogs
博客园 - 叶小钗
月光博客
月光博客
Webroot Blog
Webroot Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Help Net Security
Help Net Security
S
Secure Thoughts
SecWiki News
SecWiki News
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
S
Schneier on Security
A
Arctic Wolf
博客园 - 司徒正美
T
Tor Project blog
T
Tenable Blog
量子位
D
DataBreaches.Net
PCI Perspectives
PCI Perspectives
T
The Exploit Database - CXSecurity.com
T
Threatpost
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Cloudbric
Cloudbric
T
The Blog of Author Tim Ferriss
N
News | PayPal Newsroom
Project Zero
Project Zero
S
Securelist
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
L
LINUX DO - 热门话题
Security Archives - TechRepublic
Security Archives - TechRepublic
O
OpenAI News
C
Cisco Blogs
V
V2EX
P
Privacy & Cybersecurity Law Blog
Cisco Talos Blog
Cisco Talos Blog

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode Profile a React App for Performance
Improving the usability of your modules
2017-11-06 · via Kent C. Dodds Blog

This last week I worked on my team's internationalization (aka i18n) solution. We call it react-i18n (if we ever open source it, we'll need to rename it, because that's already taken). It's pretty neat and really small. I'm not going to talk about why we don't use any of the myriad of other tools that do this (maybe I'll save that for another blog post). What I want to talk about is something I did to make that module more usable.

One feature of the module is that it will automatically load your server-rendered content for you. At PayPal we have another module called react-content-loader. This is an express middleware that relies on conventions used in Kraken and inserts the content for the user based on their language preferences. For example, let's say that you have a file:

// locales/US/en/pages/home.properties
header.title=PayPal Rocks
header.subtitle=No really, it does

Then this middleware would insert this in the bottom of your page (for US users with en as their preferred language):

<script type="application/json" id="react-messages">
  {
    "pages/home": {
      "header": {
        "title": "PayPal Rocks",
        subtitle: "No really, it does"
      }
    }
  }
</script>

Then react-i18n will automatically load that on the client side. All you have to do is use it:

import getContentForFile from 'react-i18n'
const i18n = getContentForFile('pages/home')

function App() {
	return (
		<div>
			<h1>{i18n('header.title')}</h1>
			<div>{i18n('header.subtitle')}</div>
		</div>
	)
}

So that's how it works (again, I'm sure some of you are thinking of other libs that could do this better, but please spare me the "well actually." I'm aware of them, I promise). Now that you understand basically how this works, I want to talk about a few things that I changed about it to make it more usable.

I'll show you

No side-effects on import

So you'll notice that when we use react-i18n on the client in the example above, we don't have to do anything to initialize or bootstrap it with content. It automatically gets those from the DOM. It does this inside the main export from react-i18n. This way when you import react-i18n, loading the content happens for you. This is a handy feature. But it comes with the trade-off that the main module in react-i18nhas side-effects in the root-level of the module. For example:

// react-i18n/index.js
// ... stuff
// side-effect!
const content = JSON.parse(document.getElementById('react-messages'))
// ... more stuff
export { getContentForFile as default, init }

This presents a few challenges for users of the module. It means that they have to be aware of what happens when they import your module. They have to make sure that they don't import your module before the global environment is ready for it. And that problem manifests itself not only in the application environment, but also in the test environment! And unless you take care to give good warnings when the environment isn't ready (if you even know), people will get cryptic error messages when doing seemingly unrelated tasks (like importing some module that happens to import your module somewhere in the dependency graph).

Another issue is that there could be a reason to configure the initialization process. What if my node doesn't have the id react-messages, but instead uses i18n-content? Or what if I don't server-render the messages at all and they're coming from an ajax request? Turns out that react-i18n actually exposed another module react-i18n/bootstrap to customize this behavior which is great, but that doesn't resolve the problem of stuff happening if someone were to import react-i18n first.

So what I did was a wrapped all side-effects in a function I exported called init(which was similar to the bootstrap thing it already exported):

// react-i18n/index.js
// ... stuff
function init(options) {
	// ... other stuff
	// side-effect! But it's ok now because that's clear
	const messages = JSON.parse(document.getElementById('react-messages'))
	// ... other other stuff
}
// ... more stuff
export { getContentForFile as default, init }

So this means that anyone using the module now must call the init function, but they're doing that on their own terms and whenever they want it to happen which I think is the key difference. It doesn't matter whether someone imports this module before initialization takes place. It also gives us an opportunity to give a more informative error message if they fail to initialize before they start using the module.

The key is that your module shouldn't do side-effects when it's imported. Instead, export functions which perform the side-effects. This gives the users control over when and what happens. Even better is to not have any side-effects at all if you can help it (which is actually also possible to accomplish with my reworking of react-i18n), but that's a subject for another newsletter.

Make it generic

Before, this library was actually just a part of our app. So we could easily rely on the fact that the JSON object was a nested object where the first key was the name of the localization file and the rest was just a nested version of the contents of that file (as you can tell in the example above). And the implementation and examples in the docs were all geared toward this use case. However, we're in the process of "inner sourcing" this module (and perhaps open sourcing it eventually), so folks are going to use it who use different tools and have different use cases.

So, if it's not too much work and doesn't add too much complexity, then try to make the solution more generic. So now, the implementation doesn't care about the fact that the root level of the localization object is a file name and the rest is the contents of that file. All it cares about is the fact that it's a nested JavaScript object. This means that whereas before, you had to do this:

import getContentForFile from 'react-i18n'
const i18n = getContentForFile('pages/home')

// etc...
i18n('header.title')
// etc...

You can now do this:

import getContent from 'react-i18n'

// etc...
getContent('pages/home.header.title')
getContent('pages/home')('header.title')
getContent('pages/home.header')('title')
// etc...

So each invocation of getContent will return the content or if the content is another nested object it'll return another content getter function. I call this "sota-curried" because it's not really currying, but it kinda looks like it a little bit.

Now PayPal's react-i18n is more generically useful because the implementation and documentation don't assume you're using react-content-loader. And as it turned out, doing things this way actually made the implementation simpler! Wahoo!

I should mention also that you can't predict the future, and that's what you sort of have to try to do when building a generic library. While you're doing this, you need to balance usability with the YAGNI principle. I only put this effort in because we were extracting this from our project so others could use it and we needed to support these use cases. Beware of pre-mature optimizations (that's not limited to performance situations, but also features/complexity as well).

There were several other things I could talk about, but I'm going to wrap this email up with this. I hope that you find ways to remove side-effects from the root-level of most of your modules and find ways to make your solution more generic without sacraficing usability or implementation complexity.

Good luck! And stay awesome 😎

Who's awesome? You are!