I'm attempting to use react redux, but I'm facing an issue. The following code only renders the first letter, "T", rather than all of "Test". The Redux debugger shows all of "Test". How can this be?
Index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
import { localizeReducer } from 'react-localize-redux';
import { combineReducers, createStore } from 'redux';
import { LocalizeProvider } from "react-localize-redux";
const store = createStore(
combineReducers({localize: localizeReducer})
);
ReactDOM.render(
<LocalizeProvider store={store}>
<App />
</LocalizeProvider>,
document.getElementById('root'));
App.js:
import React, { Component } from 'react';
import { Translate, withLocalize } from "react-localize-redux";
import { renderToStaticMarkup } from 'react-dom/server';
class App extends Component {
constructor(props) {
super(props);
this.props.initialize({
languages: [
{ name: "English", code: "en" }
],
options: { renderToStaticMarkup }
});
let english = {
abc: "Test"
}
this.props.addTranslation(english);
}
render() {
return <Translate id="abc" />;
}
}
export default withLocalize(App);
If you want to add a translation for just one language, you would use addTranslationForLanguage. This one uses the single language format:
let english = {
abc: "Test"
}
this.props.addTranslationForLanguage(english, 'en');
addTranslation should be used when using multiple languages. This api requires the data in all-language-format, so you would do something like this array for multiple languages:
let translations = {
abc: ["Test"]
}
this.props.addTranslationForLanguage(translations);
Related
I am using react-syntax-highlighter#15.4.3 for syntax highlighting. Below is the code:
import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { docco } from "react-syntax-highlighter/dist/cjs/styles/hljs";
class CodeBlock extends PureComponent {
static propTypes = {
value: PropTypes.string.isRequired,
language: PropTypes.string
};
static defaultProps = {
language: null
};
render() {
const { language, value } = this.props;
return (
<SyntaxHighlighter style={docco}>
{value}
</SyntaxHighlighter>
);
}
}
export default CodeBlock;
<ReactMarkdown source={this.state.post.description} renderers={{CodeBlock}}/>
I expect it to detect the language automatically supplied to it by react-markdown, but it is not detecting the language and hence the code is not beautified.
What should I do so it starts detecting the language by itself?
I have found the below answer in the documentation of react-markdown:
import React from 'react'
import ReactMarkdown from 'react-markdown'
import SyntaxHighlighter from 'react-syntax-highlighter'
import {docco} from 'react-syntax-highlighter/dist/esm/styles/hljs'
import {render} from 'react-dom'
export const renderers = {
code: ({language, value}) => {
return <SyntaxHighlighter style={docco} language={language} children={value} />
}
}
I trying to test a React component which renders a D3 element in another class, below is the component code.
import React, {Component} from 'react';
import {graphql} from "react-apollo";
import * as compose from 'lodash.flowright';
import MostConvD3bar from './MostConvD3bar'
import MostConvProdQuery from "../../Query/MostConvProdQuery";
import style from "./MostConvProds.css"
class MostConvProds extends Component {
componentDidMount(){
if (!this.props.loading)
new MostConvD3bar(this.refs.mostConChart, this.props.products)
}
componentWillReceiveProps(nextProps){
if (!nextProps.loading)
new MostConvD3bar(this.refs.mostConChart, nextProps.products)
}
render() {
return (
<div className={style.container} ref="mostConChart"> </div>
)
}
}
const mostConvQuery = {
name: "mostConvQuery",
props({ mostConvQuery: { error, loading, mostConverted } }) {
return {
loading,
error,
products: mostConverted
}
}
}
export default compose(
graphql(MostConvProdQuery, mostConvQuery))(MostConvProds);
I have imported lodash.flowright as compose as you can see, but I am getting an error like below.
TypeError: compose is not a function
Any help will be appreciated.
Replace:
import * as compose from 'lodash.flowright';
with:
import { flowRight as compose } from 'lodash';
I'm trying to export initial component, but all the time I'm getting Invariant Violation error:
Is there any better way to export my HMAPP component and import it inside App.js?
Error image:
Error screen
Here is my App.js:
import HMAPP from './app/HMAPP';
export default HMAPP;
Here is my HMAPP component:
import { Provider } from 'react-redux';
import { persistStore } from 'redux-persist';
import { Navigation } from 'react-native-navigation';
import Mapbox from '#mapbox/react-native-mapbox-gl';
import { registerScreens } from './screens';
import store from './store/configureStore';
import { appInit, getInitialScreen } from './appInit';
import { handleErrorObject } from './lib/handleError';
Mapbox.setAccessToken('pkaeda324234');
const persistor = persistStore(
store,
null,
() => {
registerScreens(store, Provider);
appInit(store)
.then(() => {
const initialScreen = getInitialScreen(store.getState());
Navigation.startSingleScreenApp({
screen: {
screen: initialScreen,
},
passProps: {
persistor,
},
drawer: {
left: {
screen: 'DrawerMenuScreen',
},
},
appStyle: {
orientation: 'portrait',
},
});
})
.catch((error) => {
handleErrorObject('Error initializing app', error);
});
},
);
According to the docs of export and import, to externalize something inside one .js file, you need to use export. Once your module is exported, you can import him and use anywhere you want inside another .js files, for example.
So, in your HMAP.js file you'll need to export your const like this:
const persistor = persistStore( ... )
export default persistor;
and if you want to export more than one, you can export an object like this:
const persistor = persistStore( ... )
const persistor2 = persistStore2( ... )
export { persistor, persistor2 };
With your content exported, you can import it now on your App.js file:
import persistor from './app/HMAPP'; // use it when you exported with "default"
or
import { persistor1, persistor2 } from './app/HMAPP';
you could also import everything inside that file:
import * as persistors from './app/HMAPP';
Hope it helps someway.
In React Native, If you want to use your child component in other parent component then you have export that child component and import child component in parent component.
Else,
you declare your component with only class name, but in this way you can not use that component in any where.
Ex:
class Test extends React.Component {
}
I'm started to use react-native (i'm from react-js), and i'm trying to implement my first little app with a router and redux as I do in web development with reactjs.
But i met a problem, and i can't to fix it.
so i'm on this way :
Index.ios.js
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View } from 'react-native';
import { Provider } from 'react-redux'
import { NavBar, Route, Router, Schema, TabBar, TabRoute } from 'react-native-router-redux';
import configureStore from './src/store/configureStore'
const store = configureStore()
import Index from './src/components/index'
class ole extends Component {
render() {
return (
<Provider store={store}>
<Router initial="index">
<Route name="index" component={Index} />
</Router>
</Provider>
)
}
}
AppRegistry.registerComponent('ole', () => ole);
store/configureStore.js
import { createStore, applyMiddleware } from 'redux'
import promise from 'redux-promise'
import reducer from '../reducers'
export default function configureStore(initialState){
const store = createStore(
reducer,
applyMiddleware(promise)
)
return store
}
reducers/index.js
import { combineReducers } from 'redux'
const INITIAL_STATE = {
test: [],
errors: undefined
}
initTestReducer = (state = INITIAL_STATE, action) => {
return state
}
export default combineReducers({
test: initTestReducer
})
if do exactly like that i get this error in my simulator on iOS :
Super expression must either be null or a function, not undefined
but if my index.ios.js look like that, it works perfectly :
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View } from 'react-native';
import { Provider } from 'react-redux'
import { NavBar, Route, Router, Schema, TabBar, TabRoute } from 'react-native-router-redux';
import configureStore from './src/store/configureStore'
const store = configureStore()
import Index from './src/components/index'
class ole extends Component {
render() {
return (
<Provider store={store}>
<Index />
</Provider>
)
}
}
AppRegistry.registerComponent('ole', () => ole);
So error come from Router
get u an idea ?
Thank's a lot :)
I have a application structure that mostly mimics the redux real-world example but can't get past getting this error:
Uncaught Error: Invariant Violation: Could not find "store" in either the context or props of "Connect(Home)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Home)".
Here follows my application structure:
render
import { render, createFactory } from 'react'
import { Provider } from 'react-redux';
import router from './router/router'
import store from './flux/store'
window.onload = () => {
render(createFactory(Provider)({store: store},
() => router
), document.body)
};
Router
import { _ } from 'factories'
import { Router, IndexRoute, Route } from 'react-router'
import history from 'history/lib/createBrowserHistory'
import Home from '../components/home/home'
let route = _(Route),
index = _(IndexRoute);
let router = _(Router)({history: history()},
route({path: 'home', component: Home})
);
export default router;
Home Component
import React, { PropTypes } from 'react'
import { div } from 'factories'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux';
import * as users from '../../flux/actions/users'
class Home extends React.Component {
constructor() {
super();
this.state = {}
}
componentWillMount() {
console.log(this.props.users.user)
}
componentWillUnmount() {
}
render() {
return (
div({}, "Home")
)
}
}
export default connect(
state => {
return {
users: state.users
}
},
dispatch => bindActionCreators(users, dispatch)
)(Home)
This results in getting the above mentioned error. as you can see I am passing the store in the same manner as shown in the redux example.
The solution was found in redux troubleshooting docs. I needed to have my react-router return a function that created the actual router:
import { _ } from 'factories'
import { Router, IndexRoute, Route } from 'react-router'
import history from 'history/lib/createBrowserHistory'
import Home from '../components/home/home'
let route = _(Route),
index = _(IndexRoute);
const router = () =>
_(Router)({history: history()},
route({path: 'home', component: Home})
);
export default router;