npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-rmodel

v1.0.0

Published

a babel plugin of achieving bidirectional bindings of react state

Downloads

3

Readme

babel-plugin-rmodel

设计动机:react需要一个rModel

    本人在后台管理项目中使用vue比较多,私以为v-model是一个很好的语法糖,可以有效地减少了编写表单控件的输入处理的代码。后台管理系统详情页面中常用到大量的输入控件,比如商品详情页面,往往包含几十个字段的展示与编辑。react并没有在架构代码中提供类似于vue中v-model的功能,要实现相同的功能就要手动编写大量的onChange处理方法。前端的发展历程,往往很多时候是为了优雅地减少前端工程师的工作量。为了在react中能使用类似于vue的v-model这种双向绑定的语法糖,我特意写了这个babel插件,在编译的过程中自动的对代码进行转换。

简单使用

编写如下代码:

class App extends React.Component {
  constructor () {
    super();
    this.state = {
      inputVal: 8
    }
  }
  render () {
    return (
      <div className="App">
        <p>{this.state.inputVal}</p>
        <input rModel={this.state.inputVal}/>
      </div>
    );
  } 
}

转换后相当于如下代码:

class App extends React.Component {
  constructor () {
    super();
    this.state = {
      inputVal: 8
    }
  }
  render () {
    return (
      <div className="App">
        <p>{this.state.inputVal}</p>
        <input 
            value={this.state.inputVal} 
            onChange={
                e => {
                    let val = e.target.value;
                    this.setState(state => ({inputVal:val}))
                }
            }
        />
      </div>
    );
  } 
}

如果props中rModel与value同时存在,则value会被忽略

如果props中rModel与onChange同时存在,则会先执行onChange处理函数,再执行生成的改变状态的语句。

例如:

<input 
  rModel={this.state.inputVal} 
  value='testValue' 
  onChange={ 
    e => { 
      console.log(e.target.value)
    }
  }
/>

相当于

<input 
  value={this.state.inputVal} 
  onChange={ 
    e => {
      let _ref = e => { 
        console.log(e.target.value)
      }
      _ref(e);
      let val = e.target.value;
      this.setState(state => ({inputVal:val}))
    }
  }
/>

对react useState Hook的支持

function App () {
  const [inputVal, changeInputVal] = useState(0);
  return (
    <div className="App">
      <p>{inputVal}</p>
      <input
        rModel={[inputVal, changeInputVal]}
        onChange={e => console.log('onChangeHandle')} />
    </div>
  );
}

相当于

function App () {
  const [inputVal, changeInputVal] = useState(0);
  return (
    <div className="App">
      <p>{inputVal}</p>
      <input 
        value={inputVal}
        onChange={
          e => {
            console.log('onChangeHandle')
            changeInputVal(e.target.value)
          }
        } />
    </div>
  );
}

NPM 安装

npm install --save-dev babel-plugin-rmodel

如何配置

请参考babel的preset与插件的使用方法

preset: https://babeljs.io/docs/en/presets

plugin: https://babeljs.io/docs/en/plugins

如果你用create-react-app建立的项目,你可以修改package.json文件和webpack.config.js文件

package.json在presets的react-app前添加一行代码
"babel": {
    "presets": [
+       {"plugins": ["babel-plugin-rmodel"]},
        "react-app"
    ]
  },
webpack.config.js在babel-loader的plugins中添加一行代码
{
    test: /\.(js|mjs|jsx|ts|tsx)$/,
    include: paths.appSrc,
    loader: require.resolve('babel-loader'),
    options: {
    customize: require.resolve(
        'babel-preset-react-app/webpack-overrides'
    ),
    
    plugins: [
        [
        require.resolve('babel-plugin-named-asset-import'),
        {
            loaderMap: {
            svg: {
                ReactComponent:
                '@svgr/webpack?-svgo,+titleProp,+ref![path]',
            },
            },
        },
+       require.resolve('babel-plugin-rmodel'),
        ],
    ],
    // This is a feature of `babel-loader` for webpack (not Babel itself).
    // It enables caching results in ./node_modules/.cache/babel-loader/
    // directory for faster rebuilds.
    cacheDirectory: true,
    // See #6846 for context on why cacheCompression is disabled
    cacheCompression: false,
    compact: isEnvProduction,
    },
}