Как вернуться из класса JSON массива реагировать - PullRequest
0 голосов
/ 13 мая 2019

Привет, у меня есть этот код, который извлекает и возвращает данные json из файла config.json

text.js

 class Text extends React.Component {
     constructor(props){
        super(props)
        this.state = {
          datat: [],
        };
      }
      componentDidMount(){
         fetch('/config.json')
          .then(response => response.json())
          .then((datao) =>{        
            this.setState({
                datat: (JSON.parse(JSON.stringify(datao)))
            })

          });
       }
      render(){
         const datatorender = this.state.datat;
    return ( Object.keys(datatorender).map(key =>{if(key==this.props.value){return datatorender[this.props.value]}}))
        }}

, и как я его называю в домашних условиях, выглядит так: home.js

<Text value="SITENAME">

поэтому я хочу назвать это так: {text.SITENAME} вместо первого, как я могу это сделать?

и это файл json:

{
  "SITENAME": "site name",
  "SITE_DESCRIPTION":"desc"
}

Ответы [ 2 ]

0 голосов
/ 14 мая 2019

Попробуйте это:

class Text extends React.Component {
     constructor(props){
        super(props)
        this.props = props;
        this.state = {
          datat: [],
        };
      }
      componentDidMount(){
         fetch('/config.json')
          .then(response => response.json())
          .then((datao) =>{        
            this.setState({
                datat: (JSON.parse(JSON.stringify(datao)))
            })

          });
       }
      render() {
          const datatorender = this.state.datat;
          console.log(datatorender)
          return (
            <div>
              {
                Object.keys(datatorender).map((key, i) => {
                  if (key === this.props.value) {
                    return (
                      <li key={i}> {datatorender[this.props.value]} </li>
                    )
                  } else {
                    return null
                  }
                })
              }
            </div>
          )
      }
}
0 голосов
/ 14 мая 2019

Вот как вы это делаете:

// FILE: home.js
class Home extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      datat: [],
    };
  }
  render() {
    return 
    <div>
      {this.state.datat.SITENAME === undefined ? null : this.state.datat.SITENAME} // Just like you wanted to access
      <Text value="SITENAME"
            datat={this.state.datat}
            updateDatat={(val) => this.setState({datat: val})}/>
    </div>
  }
}

// FILE: text.js 
class Text extends React.Component {
  componentDidMount() {
    fetch('/config.json')
      .then(response => response.json())
      .then((datao) => {
        this.props.updateDatat({
          JSON.parse(JSON.stringify(datao))
        })
      });
  }
  render() {
    const datatorender = this.props.datat;
    return (Object.keys(datatorender).map(key => {
      if (key == this.props.value) {
        return datatorender[this.props.value]
      }
    }))
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...