higher-order-components

Send Variable to withStyles in Material UI?

匆匆过客 提交于 2019-12-02 07:42:11
问题 I have the following: class StyledInput extends React.Component{ styles = (color, theme) => ({ underline: { borderBottom: `2px solid ${color}`, '&:after': { borderBottom: `2px solid ${color}`, } } }) div = props => ( <TextField placeholder="temp input" InputProps={{ classes:{ root: props.classes.underline }, style:{ height: '1.5rem', fontSize:'1rem', marginTop: '-1rem', } }} > <div> {props.children} </div> </TextField> ) Styled = withStyles(this.styles('white'))(this.div) render(){ return(

Passing props to Higher-Order Component

妖精的绣舞 提交于 2019-11-30 23:22:12
I have a higher-order component FormBuilder like this: const FormBuilder = (WrappedComponent) => { return class HOC extends React.Component { clearForm() { // ... } render() { return ( <Form onSubmit={//what do I say here?!}> <Form.Input placeholder='Name' name='name' /> <WrappedComponent clearForm={this.clearForm} /> <Form> ); } } } And here is the WrappedComponent NewPizzaForm : class WrappedComponent extends React.Component { onSubmit() { // sends a POST request to the backend, then this.props.clearForm() } render() { return ( <Form.Button>Add Pizza</Form.Button> ); } } const NewPizzaForm =

Typescript with React - use HOC on a generic component class

眉间皱痕 提交于 2019-11-30 14:17:11
I have a generic React component, say like this one: class Foo<T> extends React.Component<FooProps<T>, FooState> { constructor(props: FooProps<T>) { super(props); render() { return <p> The result is {SomeGenericFunction<T>()}</p>; } } I also have a HOC that looks similar to this one (but is less pointless): export const withTd = <T extends WithTdProps>(TableElement: React.ComponentType<T>): React.SFC<T> => (props: T) => <td><TableElement {...props}/></td>; But when I use a component like this: const FooWithTd = withTd(Foo); There is no way to pass the type argument, as you can do neither

Typescript with React - use HOC on a generic component class

自作多情 提交于 2019-11-29 15:13:09
问题 I have a generic React component, say like this one: class Foo<T> extends React.Component<FooProps<T>, FooState> { constructor(props: FooProps<T>) { super(props); render() { return <p> The result is {SomeGenericFunction<T>()}</p>; } } I also have a HOC that looks similar to this one (but is less pointless): export const withTd = <T extends WithTdProps>(TableElement: React.ComponentType<T>): React.SFC<T> => (props: T) => <td><TableElement {...props}/></td>; But when I use a component like this

React router v4 - Authorized routes with HOC

北城以北 提交于 2019-11-29 12:02:49
I have a problem to prevent unauthorized users from accessing authorized-only routes/components - such as logged in users dashboard I have the following code: import React from 'react' //other imports import {withRouter} from 'react-router' class User extends React.Component { constructor(props) { super(props) console.log('props', props) let user = JSON.parse(localStorage.getItem('userDetails')) if(!user || !user.user || props.match.params.steamId !== user.user.steamId) { props.history.push('/') } else { this.props.updateUserState(user) this.props.getUser(user.user.steamId) } } //render

Functions are not valid as a React child. This may happen if you return a Component instead of from render

☆樱花仙子☆ 提交于 2019-11-28 20:11:08
I have written a Higher Order Component: import React from 'react'; const NewHOC = (PassedComponent) => { return class extends React.Component { render(){ return ( <div> <PassedComponent {...this.props}/> </div> ) } } } export default NewHOC; I am using the above in my App.js: import React from 'react'; import Movie from './movie/Movie'; import MyHOC from './hoc/MyHOC'; import NewHOC from './hoc/NewHOC'; export default class App extends React.Component { render() { return ( <div> Hello From React!! <NewHOC> <Movie name="Blade Runner"></Movie> </NewHOC> </div> ); } } But, the warning I am

Functions are not valid as a React child. This may happen if you return a Component instead of from render

99封情书 提交于 2019-11-27 01:01:09
问题 I have written a Higher Order Component: import React from 'react'; const NewHOC = (PassedComponent) => { return class extends React.Component { render(){ return ( <div> <PassedComponent {...this.props}/> </div> ) } } } export default NewHOC; I am using the above in my App.js: import React from 'react'; import Movie from './movie/Movie'; import MyHOC from './hoc/MyHOC'; import NewHOC from './hoc/NewHOC'; export default class App extends React.Component { render() { return ( <div> Hello From