react-native转react-web, react+redux, webpack打包
index.js
// Import React when the system is web import React from 'react'; import { AppRegistry, View } from 'react-native' import { createStore } from 'redux' import { Provider } from 'react-redux' import { persistStore, autoRehydrate } from 'redux-persist' // Import the reducer and create a store import { reducer } from './todoListRedux' // Add the autoRehydrate middleware to your redux store const store = createStore(reducer, undefined, autoRehydrate()) // Enable persistence persistStore(store) // Import the App container component import App from './App' // Pass the store into the Provider const AppWithStore = () => ( <Provider store={store}> <App /> </Provider> ) AppRegistry.registerComponent('App', () => AppWithStore) // Import React when the system is web AppRegistry.runApplication('App', { initialProps: {}, rootTag: document.getElementById('react-app') })
App.js
import React, { Component } from 'react' import { AppRegistry, View } from 'react-native' import { connect } from 'react-redux' import { actionCreators } from './todoListRedux' import List from './List' import Input from './Input' import Title from './Title' const mapStateToProps = (state) => ({ todos: state.todos, }) class App extends Component { onAddTodo = (text) => { const {dispatch} = this.props dispatch(actionCreators.add(text)) } onRemoveTodo = (index) => { const {dispatch} = this.props dispatch(actionCreators.remove(index)) } render() { const {todos} = this.props return ( <View> <Title> To-Do List </Title> <Input placeholder={'Type a todo, then hit enter!'} onSubmitEditing={this.onAddTodo} /> <List list={todos} onPressItem={this.onRemoveTodo} /> </View> ) } } export default connect(mapStateToProps)(App)
webpack.config.js
var path = require('path') var webpack = require('webpack') module.exports = { entry: './index.js', output: { path: path.resolve(__dirname, './dist'), publicPath: '/dist/', filename: 'build.js' }, module: { loaders: [ // { // test: /\.js$/, // loader: 'jsx-loader?harmony' // }, { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader', query: { presets: ['es2015', 'react', 'stage-0'] // plugins: ['transform-runtime'] } } ] }, resolve: { alias: { 'react-native': 'react-native-web' }, extensions:['','.js','.json'] } }
相关推荐
不知道该写啥QAQ 2020-08-02
不知道该写啥QAQ 2020-06-10
不知道该写啥QAQ 2020-11-12
webfullStack 2020-11-09
Yvettre 2020-09-15
想做大牛的蜗牛 2020-10-30
gloria0 2020-10-26
gaojie0 2020-09-11
SelinaChan 2020-08-14
不知道该写啥QAQ 2020-08-09
gloria0 2020-08-09
hline 2020-07-29
SelinaChan 2020-07-28
wangdianyong 2020-07-23
webpackvuees 2020-07-23
yqoxygen 2020-07-20
不知道该写啥QAQ 2020-07-18
waterv 2020-07-18